aboutsummaryrefslogtreecommitdiffstats
path: root/kernel-shark/src/libkshark-model.c
blob: 6c54e1e172736383637d97e46f8edea299a76f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
// SPDX-License-Identifier: LGPL-2.1

/*
 * Copyright (C) 2017 VMware Inc, Yordan Karadzhov <y.karadz@gmail.com>
 */

 /**
  *  @file    libkshark-model.c
  *  @brief   Visualization model for FTRACE (trace-cmd) data.
  */

// C
#include <stdlib.h>
#include <assert.h>

// KernelShark
#include "libkshark-model.h"

/** The index of the Upper Overflow bin. */
#define UOB(histo) (histo->n_bins)

/** The index of the Lower Overflow bin. */
#define LOB(histo) (histo->n_bins + 1)

/** For all bins. */
# define ALLB(histo) LOB(histo)

/**
 * @brief Initialize the Visualization model.
 *
 * @param histo: Input location for the model descriptor.
 */
void ksmodel_init(struct kshark_trace_histo *histo)
{
	/*
	 * Initialize an empty histo. The histo will have no bins and will
	 * contain no data.
	 */
	memset(histo, 0, sizeof(*histo));
}

/**
 * @brief Clear (reset) the Visualization model.
 *
 * @param histo: Input location for the model descriptor.
 */
void ksmodel_clear(struct kshark_trace_histo *histo)
{
	/* Reset the histo. It will have no bins and will contain no data. */
	free(histo->map);
	free(histo->bin_count);
	ksmodel_init(histo);
}

static void ksmodel_reset_bins(struct kshark_trace_histo *histo,
			       size_t first, size_t last)
{
	/*
	 * Reset the content of the bins.
	 * Be careful here! Resetting the entire array of signed integers with
	 * memset() will work only for values of "0" and "-1". Hence
	 * KS_EMPTY_BIN is expected to be "-1".
	 */
	memset(&histo->map[first], KS_EMPTY_BIN,
	       (last - first + 1) * sizeof(histo->map[0]));

	memset(&histo->bin_count[first], 0,
	       (last - first + 1) * sizeof(histo->bin_count[0]));
}

static bool ksmodel_histo_alloc(struct kshark_trace_histo *histo, size_t n)
{
	free(histo->bin_count);
	free(histo->map);

	/* Create bins. Two overflow bins are added. */
	histo->map = calloc(n + 2, sizeof(*histo->map));
	histo->bin_count = calloc(n + 2, sizeof(*histo->bin_count));

	if (!histo->map || !histo->bin_count) {
		ksmodel_clear(histo);
		fprintf(stderr, "Failed to allocate memory for a histo.\n");
		return false;
	}

	histo->n_bins = n;

	return true;
}

static void ksmodel_set_in_range_bining(struct kshark_trace_histo *histo,
					size_t n, uint64_t min, uint64_t max,
					bool force_in_range)
{
	uint64_t corrected_range, delta_range, range = max - min;
	struct kshark_entry *last;

	/* The size of the bin must be >= 1, hence the range must be >= n. */
	if (n == 0 || range < n) {
		range = n;
		max = min + n;
	}

	/*
	 * If the number of bins changes, allocate memory for the descriptor of
	 * the model.
	 */
	if (n != histo->n_bins) {
		if (!ksmodel_histo_alloc(histo, n)) {
			ksmodel_clear(histo);
			return;
		}
	}

	/* Reset the content of all bins (including overflow bins) to zero. */
	ksmodel_reset_bins(histo, 0, ALLB(histo));

	if (range % n == 0) {
		/*
		 * The range is multiple of the number of bin and needs no
		 * adjustment. This is very unlikely to happen but still ...
		 */
		histo->min = min;
		histo->max = max;
		histo->bin_size = range / n;
	} else {
		/*
		 * The range needs adjustment. The new range will be slightly
		 * bigger, compared to the requested one.
		 */
		histo->bin_size = range / n + 1;
		corrected_range = histo->bin_size * n;
		delta_range = corrected_range - range;
		histo->min = min - delta_range / 2;
		histo->max = histo->min + corrected_range;

		if (!force_in_range)
			return;

		/*
		 * Make sure that the new range doesn't go outside of the time
		 * interval of the dataset.
		 */
		last = histo->data[histo->data_size - 1];
		if (histo->min < histo->data[0]->ts) {
			histo->min = histo->data[0]->ts;
			histo->max = histo->min + corrected_range;
		} else if (histo->max > last->ts) {
			histo->max = last->ts;
			histo->min = histo->max - corrected_range;
		}
	}
}

/**
 * @brief Prepare the bining of the Visualization model.
 *
 * @param histo: Input location for the model descriptor.
 * @param n: Number of bins.
 * @param min: Lower edge of the time-window to be visualized.
 * @param max: Upper edge of the time-window to be visualized.
 */
void ksmodel_set_bining(struct kshark_trace_histo *histo,
			size_t n, uint64_t min, uint64_t max)
{
	ksmodel_set_in_range_bining(histo, n, min, max, false);
}

static size_t ksmodel_set_lower_edge(struct kshark_trace_histo *histo)
{
	/*
	 * Find the index of the first entry inside the range
	 * (timestamp >= min). Note that the value of "min" is considered
	 * inside the range.
	 */
	ssize_t row = kshark_find_entry_by_time(histo->min,
						histo->data,
						0,
						histo->data_size - 1);

	assert(row != BSEARCH_ALL_SMALLER);

	if (row == BSEARCH_ALL_GREATER || row == 0) {
		/* Lower Overflow bin is empty. */
		histo->map[LOB(histo)] = KS_EMPTY_BIN;
		histo->bin_count[LOB(histo)] = 0;
		row = 0;
	} else {
		/*
		 * The first entry inside the range is not the first entry of
		 * the dataset. This means that the Lower Overflow bin contains
		 * data.
		 */

		/* Lower Overflow bin starts at "0". */
		histo->map[LOB(histo)] = 0;

		/*
		 * The number of entries inside the Lower Overflow bin is equal
		 * to the index of the first entry inside the range.
		 */
		histo->bin_count[LOB(histo)] = row;
	}

	/*
	 * Now check if the first entry inside the range falls into the first
	 * bin.
	 */
	if (histo->data[row]->ts < histo->min + histo->bin_size) {
		/*
		 * It is inside the first bin. Set the beginning
		 * of the first bin.
		 */
		histo->map[0] = row;
	} else {
		/* The first bin is empty. */
		histo->map[0] = KS_EMPTY_BIN;
	}

	return row;
}

static size_t ksmodel_set_upper_edge(struct kshark_trace_histo *histo)
{
	/*
	 * Find the index of the first entry outside the range
	 * (timestamp > max). Note that the value of "max" is considered inside
	 * the range. Remember that kshark_find_entry_by_time returns the first
	 * entry which is equal or greater than the reference time.
	 */
	ssize_t row = kshark_find_entry_by_time(histo->max + 1,
						histo->data,
						0,
						histo->data_size - 1);

	assert(row != BSEARCH_ALL_GREATER);

	if (row == BSEARCH_ALL_SMALLER) {
		/* Upper Overflow bin is empty. */
		histo->map[UOB(histo)] = KS_EMPTY_BIN;
		histo->bin_count[UOB(histo)] = 0;
	} else {
		/*
		 * The Upper Overflow bin contains data. Set its beginning and
		 * the number of entries.
		 */
		histo->map[UOB(histo)] = row;
		histo->bin_count[UOB(histo)] = histo->data_size - row;
	}

	return row;
}

static void ksmodel_set_next_bin_edge(struct kshark_trace_histo *histo,
				      size_t bin, size_t last_row)
{
	uint64_t time_min, time_max;
	size_t next_bin = bin + 1;
	ssize_t row;

	/* Calculate the beginning and the end of the next bin. */
	time_min = histo->min + next_bin * histo->bin_size;
	time_max = time_min + histo->bin_size;
	/*
	 * The timestamp of the very last entry of the dataset can be exactly
	 * equal to the value of the upper edge of the range. This is very
	 * likely to happen when we use ksmodel_set_in_range_bining(). In this
	 * case we have to increase the size of the very last bin in order to
	 * make sure that the last entry of the dataset will fall into it.
	 */
	if (next_bin == histo->n_bins - 1)
		++time_max;

	/*
	 * Find the index of the first entry inside
	 * the next bin (timestamp > time_min).
	 */
	row = kshark_find_entry_by_time(time_min, histo->data, last_row,
					histo->data_size - 1);

	if (row < 0 || histo->data[row]->ts >= time_max) {
		/* The bin is empty. */
		histo->map[next_bin] = KS_EMPTY_BIN;
		return;
	}

	/* Set the index of the first entry. */
	histo->map[next_bin] = row;
}

/*
 * Fill in the bin_count array, which maps the number of entries within each
 * bin.
 */
static void ksmodel_set_bin_counts(struct kshark_trace_histo *histo)
{
	int i = 0, prev_not_empty;
	ssize_t count_tmp = 0;

	histo->tot_count = 0;
	memset(&histo->bin_count[0], 0,
	       (histo->n_bins) * sizeof(histo->bin_count[0]));
	/*
	 * Find the first bin which contains data. Start by checking the Lower
	 * Overflow bin.
	 */
	if (histo->map[LOB(histo)] != KS_EMPTY_BIN) {
		prev_not_empty = LOB(histo);
	} else {
		/* Loop till the first non-empty bin. */
		while (histo->map[i] < 0 && i < histo->n_bins) {
			++i;
		}

		prev_not_empty = i++;
	}

	/*
	 * Starting from the first not empty bin, loop over all bins and fill
	 * in the bin_count array to hold the number of entries in each bin.
	 */
	for (; i < histo->n_bins; ++i) {
		if (histo->map[i] != KS_EMPTY_BIN) {
			/*
			 * The current bin is not empty, take its data row and
			 * subtract it from the data row of the previous not
			 * empty bin, which will give us the number of data
			 * rows in the "prev_not_empty" bin.
			 */
			count_tmp = histo->map[i] - histo->map[prev_not_empty];

			/*
			 * We will do a sanity check. The number of data rows
			 * in the previous not empty bin must be greater than
			 * zero.
			 */
			assert(count_tmp > 0);
			histo->bin_count[prev_not_empty] = count_tmp;

			if (prev_not_empty != LOB(histo))
				histo->tot_count += count_tmp;

			prev_not_empty = i;
		}
	}

	/* Check if the Upper Overflow bin contains data. */
	if (histo->map[UOB(histo)] == KS_EMPTY_BIN) {
		/*
		 * The Upper Overflow bin is empty. Use the size of the dataset
		 * to calculate the content of the previouse not empty bin.
		 */
		count_tmp = histo->data_size - histo->map[prev_not_empty];
	} else {
		/*
		 * Use the index of the first entry inside the Upper Overflow
		 * bin to calculate the content of the previouse not empty
		 * bin.
		 */
		count_tmp = histo->map[UOB(histo)] - histo->map[prev_not_empty];
	}

	/*
	 * We will do a sanity check. The number of data rows in the last not
	 * empty bin must be greater than zero.
	 */
	assert(count_tmp >= 0);
	histo->tot_count += histo->bin_count[prev_not_empty] = count_tmp;
}

/**
 * @brief Provide the Visualization model with data. Calculate the current
 *	  state of the model.
 *
 * @param histo: Input location for the model descriptor.
 * @param data: Input location for the trace data.
 * @param n: Number of bins.
 */
void ksmodel_fill(struct kshark_trace_histo *histo,
		  struct kshark_entry **data, size_t n)
{
	size_t last_row = 0;
	int bin;

	histo->data_size = n;
	histo->data = data;

	if (histo->n_bins == 0 ||
	    histo->bin_size == 0 ||
	    histo->data_size == 0) {
		/*
		 * Something is wrong with this histo.
		 * Most likely the binning is not set.
		 */
		ksmodel_clear(histo);
		fprintf(stderr,
			"Unable to fill the model with data.\n");
		fprintf(stderr,
			"Try to set the bining of the model first.\n");

		return;
	}

	/* Set the Lower Overflow bin */
	ksmodel_set_lower_edge(histo);

	/*
	 * Loop over the dataset and set the beginning of all individual bins.
	 */
	for (bin = 0; bin < histo->n_bins; ++bin) {
		ksmodel_set_next_bin_edge(histo, bin, last_row);
		if (histo->map[bin + 1] > 0)
			last_row = histo->map[bin + 1];
	}

	/* Set the Upper Overflow bin. */
	ksmodel_set_upper_edge(histo);

	/* Calculate the number of entries in each bin. */
	ksmodel_set_bin_counts(histo);
}

/**
 * @brief Get the total number of entries in a given bin.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 *
 * @returns The number of entries in this bin.
 */
size_t ksmodel_bin_count(struct kshark_trace_histo *histo, int bin)
{
	if (bin >= 0 && bin < histo->n_bins)
		return histo->bin_count[bin];

	if (bin == UPPER_OVERFLOW_BIN)
		return histo->bin_count[UOB(histo)];

	if (bin == LOWER_OVERFLOW_BIN)
		return histo->bin_count[LOB(histo)];

	return 0;
}

/**
 * @brief Shift the time-window of the model forward. Recalculate the current
 *	  state of the model.
 *
 * @param histo: Input location for the model descriptor.
 * @param n: Number of bins to shift.
 */
void ksmodel_shift_forward(struct kshark_trace_histo *histo, size_t n)
{
	size_t last_row = 0;
	int bin;

	if (!histo->data_size)
		return;

	if (histo->map[UOB(histo)] == KS_EMPTY_BIN) {
		/*
		 * The Upper Overflow bin is empty. This means that we are at
		 * the upper edge of the dataset already. Do nothing in this
		 * case.
		 */
		return;
	}

	histo->min += n * histo->bin_size;
	histo->max += n * histo->bin_size;

	if (n >= histo->n_bins) {
		/*
		 * No overlap between the new and the old ranges. Recalculate
		 * all bins from scratch. First calculate the new range.
		 */
		ksmodel_set_bining(histo, histo->n_bins, histo->min,
							 histo->max);

		ksmodel_fill(histo, histo->data, histo->data_size);
		return;
	}

	/* Set the new Lower Overflow bin. */
	ksmodel_set_lower_edge(histo);

	/*
	 * Copy the the mapping indexes of all overlaping bins starting from
	 * bin "0" of the new histo. Note that the number of overlaping bins
	 * is histo->n_bins - n.
	 * We will do a sanity check. ksmodel_set_lower_edge() sets map[0]
	 * index of the new histo. This index should then be equal to map[n]
	 * index of the old histo.
	 */
	assert (histo->map[0] == histo->map[n]);
	memmove(&histo->map[0], &histo->map[n],
		sizeof(histo->map[0]) * (histo->n_bins - n));

	/*
	 * Calculate only the content of the new (non-overlapping) bins.
	 * Start from the last copied bin and set the edge of each consecutive
	 * bin.
	 */
	bin = histo->n_bins - n - 1;
	for (; bin < histo->n_bins - 1; ++bin) {
		/*
		 * Note that this function will set the bin having index
		 * "bin + 1".
		 */
		ksmodel_set_next_bin_edge(histo, bin, last_row);
		if (histo->map[bin + 1] > 0)
			last_row = histo->map[bin + 1];
	}

	/*
	 * Set the new Upper Overflow bin and calculate the number of entries
	 * in each bin.
	 */
	ksmodel_set_upper_edge(histo);
	ksmodel_set_bin_counts(histo);
}

/**
 * @brief Shift the time-window of the model backward. Recalculate the current
 *	  state of the model.
 *
 * @param histo: Input location for the model descriptor.
 * @param n: Number of bins to shift.
 */
void ksmodel_shift_backward(struct kshark_trace_histo *histo, size_t n)
{
	size_t last_row = 0;
	int bin;

	if (!histo->data_size)
		return;

	if (histo->map[LOB(histo)] == KS_EMPTY_BIN) {
		/*
		 * The Lower Overflow bin is empty. This means that we are at
		 * the Lower edge of the dataset already. Do nothing in this
		 * case.
		 */
		return;
	}

	histo->min -= n * histo->bin_size;
	histo->max -= n * histo->bin_size;

	if (n >= histo->n_bins) {
		/*
		 * No overlap between the new and the old range. Recalculate
		 * all bins from scratch. First calculate the new range.
		 */
		ksmodel_set_bining(histo, histo->n_bins, histo->min,
							 histo->max);

		ksmodel_fill(histo, histo->data, histo->data_size);
		return;
	}

	/*
	 * Copy the mapping indexes of all overlaping bins starting from
	 * bin "0" of the old histo. Note that the number of overlaping bins
	 * is histo->n_bins - n.
	 */
	memmove(&histo->map[n], &histo->map[0],
		sizeof(histo->map[0]) * (histo->n_bins - n));

	/* Set the new Lower Overflow bin. */
	ksmodel_set_lower_edge(histo);

	/* Calculate only the content of the new (non-overlapping) bins. */
	for (bin = 0; bin < n - 1; ++bin) {
		/*
		 * Note that this function will set the bin having index
		 * "bin + 1".
		 */
		ksmodel_set_next_bin_edge(histo, bin, last_row);
		if (histo->map[bin + 1] > 0)
			last_row = histo->map[bin + 1];
	}

	/*
	 * Set the new Upper Overflow bin and calculate the number of entries
	 * in each bin.
	 */
	ksmodel_set_upper_edge(histo);
	ksmodel_set_bin_counts(histo);
}

/**
 * @brief Move the time-window of the model to a given location. Recalculate
 *	  the current state of the model.
 *
 * @param histo: Input location for the model descriptor.
 * @param ts: position in time to be visualized.
 */
void ksmodel_jump_to(struct kshark_trace_histo *histo, uint64_t ts)
{
	uint64_t min, max, range_min;

	if (ts > histo->min && ts < histo->max) {
		/*
		 * The new position is already inside the range.
		 * Do nothing in this case.
		 */
		return;
	}

	/*
	 * Calculate the new range without changing the size and the number
	 * of bins.
	 */
	min = ts - histo->n_bins * histo->bin_size / 2;

	/* Make sure that the range does not go outside of the dataset. */
	if (min < histo->data[0]->ts) {
		min = histo->data[0]->ts;
	} else {
		range_min = histo->data[histo->data_size - 1]->ts -
			    histo->n_bins * histo->bin_size;

		if (min > range_min)
			min = range_min;
	}

	max = min + histo->n_bins * histo->bin_size;

	/* Use the new range to recalculate all bins from scratch. */
	ksmodel_set_bining(histo, histo->n_bins, min, max);
	ksmodel_fill(histo, histo->data, histo->data_size);
}

static void ksmodel_zoom(struct kshark_trace_histo *histo,
			 double r, int mark, bool zoom_in)
{
	size_t range, min, max, delta_min;
	double delta_tot;

	if (!histo->data_size)
		return;

	/*
	 * If the marker is not set, assume that the focal point of the zoom
	 * is the center of the range.
	 */
	if (mark < 0)
		mark = histo->n_bins / 2;

	range = histo->max - histo->min;

	/*
	 * Avoid overzooming. If needed, adjust the Scale factor to a the value
	 * which provides bin_size >= 5.
	 */
	if (zoom_in && (size_t) (range * (1. - r)) < histo->n_bins * 5)
		r = 1. - (histo->n_bins * 5.) / range;

	/*
	 * Now calculate the new range of the histo. Use the bin of the marker
	 * as a focal point for the zoomout. With this the maker will stay
	 * inside the same bin in the new histo.
	 *
	 * First we set delta_tot to increase by the percentage requested (r).
	 * Then we make delta_min equal to a percentage of delta_tot based on
	 * where the position of the mark is. After this we add / subtract the
	 * original min by the delta_min and subtract / add the max by
	 * delta_tot - delta_min.
	 */
	delta_tot = range * r;

	if (mark == (int)histo->n_bins - 1)
		delta_min = delta_tot;
	else
		delta_min = delta_tot * mark / histo->n_bins;

	min = zoom_in ? histo->min + delta_min :
			histo->min - delta_min;

	max = zoom_in ? histo->max - (size_t) delta_tot + delta_min :
			histo->max + (size_t) delta_tot - delta_min;


	/* Make sure the new range doesn't go outside of the dataset. */
	if (min < histo->data[0]->ts)
		min = histo->data[0]->ts;

	if (max > histo->data[histo->data_size - 1]->ts)
		max = histo->data[histo->data_size - 1]->ts;

	/*
	 * Use the new range to recalculate all bins from scratch. Enforce
	 * "In Range" adjustment of the range of the model, in order to avoid
	 * slowly drifting outside of the data-set in the case when the very
	 * first or the very last entry is used as a focal point.
	 */
	ksmodel_set_in_range_bining(histo, histo->n_bins, min, max, true);
	ksmodel_fill(histo, histo->data, histo->data_size);
}

/**
 * @brief Extend the time-window of the model. Recalculate the current state
 *	  of the model.
 *
 * @param histo: Input location for the model descriptor.
 * @param r: Scale factor of the zoom-out.
 * @param mark: Focus point of the zoom-out.
 */
void ksmodel_zoom_out(struct kshark_trace_histo *histo,
		      double r, int mark)
{
	ksmodel_zoom(histo, r, mark, false);
}

/**
 * @brief Shrink the time-window of the model. Recalculate the current state
 *	  of the model.
 *
 * @param histo: Input location for the model descriptor.
 * @param r: Scale factor of the zoom-in.
 * @param mark: Focus point of the zoom-in.
 */
void ksmodel_zoom_in(struct kshark_trace_histo *histo,
		     double r, int mark)
{
	ksmodel_zoom(histo, r, mark, true);
}

/**
 * @brief Get the index of the first entry in a given bin.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 *
 * @returns Index of the first entry in this bin. If the bin is empty the
 *	    function returns negative error identifier (KS_EMPTY_BIN).
 */
ssize_t ksmodel_first_index_at_bin(struct kshark_trace_histo *histo, int bin)
{
	if (bin >= 0 && bin < (int) histo->n_bins)
		return histo->map[bin];

	if (bin == UPPER_OVERFLOW_BIN)
		return histo->map[UOB(histo)];

	if (bin == LOWER_OVERFLOW_BIN)
		return histo->map[LOB(histo)];

	return KS_EMPTY_BIN;
}

/**
 * @brief Get the index of the last entry in a given bin.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 *
 * @returns Index of the last entry in this bin. If the bin is empty the
 *	    function returns negative error identifier (KS_EMPTY_BIN).
 */
ssize_t ksmodel_last_index_at_bin(struct kshark_trace_histo *histo, int bin)
{
	ssize_t index = ksmodel_first_index_at_bin(histo, bin);
	size_t count = ksmodel_bin_count(histo, bin);

	if (index >= 0 && count)
		index += count - 1;

	return index;
}

static bool ksmodel_is_visible(struct kshark_entry *e)
{
	if ((e->visible & KS_GRAPH_VIEW_FILTER_MASK) &&
	    (e->visible & KS_EVENT_VIEW_FILTER_MASK))
		return true;

	return false;
}

static struct kshark_entry_request *
ksmodel_entry_front_request_alloc(struct kshark_trace_histo *histo,
				  int bin, bool vis_only,
				  matching_condition_func func, int val)
{
	size_t first, n;

	/* Get the number of entries in this bin. */
	n = ksmodel_bin_count(histo, bin);
	if (!n)
		return NULL;

	first = ksmodel_first_index_at_bin(histo, bin);

	return kshark_entry_request_alloc(first, n,
					  func, val,
					  vis_only, KS_GRAPH_VIEW_FILTER_MASK);
}

static struct kshark_entry_request *
ksmodel_entry_back_request_alloc(struct kshark_trace_histo *histo,
				 int bin, bool vis_only,
				 matching_condition_func func, int val)
{
	size_t first, n;

	/* Get the number of entries in this bin. */
	n = ksmodel_bin_count(histo, bin);
	if (!n)
		return NULL;

	first = ksmodel_last_index_at_bin(histo, bin);

	return kshark_entry_request_alloc(first, n,
					  func, val,
					  vis_only, KS_GRAPH_VIEW_FILTER_MASK);
}

/**
 * @brief Get the index of the first entry from a given Cpu in a given bin.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param cpu: Cpu Id.
 *
 * @returns Index of the first entry from a given Cpu in this bin.
 */
ssize_t ksmodel_first_index_at_cpu(struct kshark_trace_histo *histo,
				   int bin, int cpu)
{
	size_t i, n, first, not_found = KS_EMPTY_BIN;

	n = ksmodel_bin_count(histo, bin);
	if (!n)
		return not_found;

	first = ksmodel_first_index_at_bin(histo, bin);

	for (i = first; i < first + n; ++i) {
		if (histo->data[i]->cpu == cpu) {
			if (ksmodel_is_visible(histo->data[i]))
				return i;
			else
				not_found = KS_FILTERED_BIN;
		}
	}

	return not_found;
}

/**
 * @brief Get the index of the first entry from a given Task in a given bin.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param pid: Process Id of a task.
 *
 * @returns Index of the first entry from a given Task in this bin.
 */
ssize_t ksmodel_first_index_at_pid(struct kshark_trace_histo *histo,
				   int bin, int pid)
{
	size_t i, n, first, not_found = KS_EMPTY_BIN;

	n = ksmodel_bin_count(histo, bin);
	if (!n)
		return not_found;

	first = ksmodel_first_index_at_bin(histo, bin);

	for (i = first; i < first + n; ++i) {
		if (histo->data[i]->pid == pid) {
			if (ksmodel_is_visible(histo->data[i]))
				return i;
			else
				not_found = KS_FILTERED_BIN;
		}
	}

	return not_found;
}

/**
 * @brief In a given bin, start from the front end of the bin and go towards
 *	  the back end, searching for an entry satisfying the Matching
 *	  condition defined by a Matching condition function.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param vis_only: If true, a visible entry is requested.
 * @param func: Matching condition function.
 * @param val: Matching condition value, used by the Matching condition
 *	       function.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns Pointer ot a kshark_entry, if an entry has been found. Else NULL.
 */
const struct kshark_entry *
ksmodel_get_entry_front(struct kshark_trace_histo *histo,
			int bin, bool vis_only,
			matching_condition_func func, int val,
			struct kshark_entry_collection *col,
			ssize_t *index)
{
	struct kshark_entry_request *req;
	const struct kshark_entry *entry;

	if (index)
		*index = KS_EMPTY_BIN;

	/* Set the position at the beginning of the bin and go forward. */
	req = ksmodel_entry_front_request_alloc(histo, bin, vis_only,
							    func, val);
	if (!req)
		return NULL;

	if (col && col->size)
		entry = kshark_get_collection_entry_front(&req, histo->data,
							  col, index);
	else
		entry = kshark_get_entry_front(req, histo->data, index);

	kshark_free_entry_request(req);

	return entry;
}

/**
 * @brief In a given bin, start from the back end of the bin and go towards
 *	  the front end, searching for an entry satisfying the Matching
 *	  condition defined by a Matching condition function.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param vis_only: If true, a visible entry is requested.
 * @param func: Matching condition function.
 * @param val: Matching condition value, used by the Matching condition
 *	       function.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns Pointer ot a kshark_entry, if an entry has been found. Else NULL.
 */
const struct kshark_entry *
ksmodel_get_entry_back(struct kshark_trace_histo *histo,
		       int bin, bool vis_only,
		       matching_condition_func func, int val,
		       struct kshark_entry_collection *col,
		       ssize_t *index)
{
	struct kshark_entry_request *req;
	const struct kshark_entry *entry;

	if (index)
		*index = KS_EMPTY_BIN;

	/* Set the position at the end of the bin and go backwards. */
	req = ksmodel_entry_back_request_alloc(histo, bin, vis_only,
							   func, val);
	if (!req)
		return NULL;

	if (col && col->size)
		entry = kshark_get_collection_entry_back(&req, histo->data,
							  col, index);
	else
		entry = kshark_get_entry_back(req, histo->data, index);

	kshark_free_entry_request(req);

	return entry;
}

static int ksmodel_get_entry_pid(const struct kshark_entry *entry)
{
	if (!entry) {
		/* No data has been found. */
		return KS_EMPTY_BIN;
	}

	/*
	 * Note that if some data has been found, but this data is
	 * filtered-outa, the Dummy entry is returned. The PID of the Dummy
	 * entry is KS_FILTERED_BIN.
	 */

	return entry->pid;
}

/**
 * @brief In a given bin, start from the front end of the bin and go towards
 *	  the back end, searching for an entry from a given CPU. Return
 *	  the Process Id of the task of the entry found.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param cpu: CPU Id.
 * @param vis_only: If true, a visible entry is requested.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns Process Id of the task if an entry has been found. Else a negative
 *	    Identifier (KS_EMPTY_BIN or KS_FILTERED_BIN).
 */
int ksmodel_get_pid_front(struct kshark_trace_histo *histo,
			  int bin, int cpu, bool vis_only,
			  struct kshark_entry_collection *col,
			  ssize_t *index)
{
	const struct kshark_entry *entry;

	if (cpu < 0)
		return KS_EMPTY_BIN;

	entry = ksmodel_get_entry_front(histo, bin, vis_only,
					       kshark_match_cpu, cpu,
					       col, index);

	return ksmodel_get_entry_pid(entry);
}

/**
 * @brief In a given bin, start from the back end of the bin and go towards
 *	  the front end, searching for an entry from a given CPU. Return
 *	  the Process Id of the task of the entry found.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param cpu: CPU Id.
 * @param vis_only: If true, a visible entry is requested.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns Process Id of the task if an entry has been found. Else a negative
 *	    Identifier (KS_EMPTY_BIN or KS_FILTERED_BIN).
 */
int ksmodel_get_pid_back(struct kshark_trace_histo *histo,
			 int bin, int cpu, bool vis_only,
			 struct kshark_entry_collection *col,
			 ssize_t *index)
{
	const struct kshark_entry *entry;

	if (cpu < 0)
		return KS_EMPTY_BIN;

	entry = ksmodel_get_entry_back(histo, bin, vis_only,
					      kshark_match_cpu, cpu,
					      col, index);

	return ksmodel_get_entry_pid(entry);
}

static int ksmodel_get_entry_cpu(const struct kshark_entry *entry)
{
	if (!entry) {
		/* No data has been found. */
		return KS_EMPTY_BIN;
	}

	/*
	 * Note that if some data has been found, but this data is
	 * filtered-outa, the Dummy entry is returned. The CPU Id of the Dummy
	 * entry is KS_FILTERED_BIN.
	 */

	return entry->cpu;
}

/**
 * @brief In a given bin, start from the front end of the bin and go towards
 *	  the back end, searching for an entry from a given PID. Return
 *	  the CPU Id of the entry found.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param pid: Process Id.
 * @param vis_only: If true, a visible entry is requested.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns The CPU Id of the task if an entry has been found. Else a negative
 *	    Identifier (KS_EMPTY_BIN or KS_FILTERED_BIN).
 */
int ksmodel_get_cpu_front(struct kshark_trace_histo *histo,
			  int bin, int pid, bool vis_only,
			  struct kshark_entry_collection *col,
			  ssize_t *index)
{
	const struct kshark_entry *entry;

	if (pid < 0)
		return KS_EMPTY_BIN;

	entry = ksmodel_get_entry_front(histo, bin, vis_only,
					       kshark_match_pid, pid,
					       col,
					       index);
	return ksmodel_get_entry_cpu(entry);
}

/**
 * @brief In a given bin, start from the back end of the bin and go towards
 *	  the front end, searching for an entry from a given PID. Return
 *	  the CPU Id of the entry found.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param pid: Process Id.
 * @param vis_only: If true, a visible entry is requested.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns The CPU Id of the task if an entry has been found. Else a negative
 *	    Identifier (KS_EMPTY_BIN or KS_FILTERED_BIN).
 */
int ksmodel_get_cpu_back(struct kshark_trace_histo *histo,
			 int bin, int pid, bool vis_only,
			 struct kshark_entry_collection *col,
			 ssize_t *index)
{
	const struct kshark_entry *entry;

	if (pid < 0)
		return KS_EMPTY_BIN;

	entry = ksmodel_get_entry_back(histo, bin, vis_only,
					      kshark_match_pid, pid,
					      col,
					      index);

	return ksmodel_get_entry_cpu(entry);
}

/**
 * @brief Check if a visible trace event from a given Cpu exists in this bin.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param cpu: Cpu Id.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns True, if a visible entry exists in this bin. Else false.
 */
bool ksmodel_cpu_visible_event_exist(struct kshark_trace_histo *histo,
				     int bin, int cpu,
				     struct kshark_entry_collection *col,
				     ssize_t *index)
{
	struct kshark_entry_request *req;
	const struct kshark_entry *entry;

	if (index)
		*index = KS_EMPTY_BIN;

	/* Set the position at the beginning of the bin and go forward. */
	req = ksmodel_entry_front_request_alloc(histo,
						bin, true,
						kshark_match_cpu, cpu);
	if (!req)
		return false;

	/*
	 * The default visibility mask of the Model Data request is
	 * KS_GRAPH_VIEW_FILTER_MASK. Change the mask to
	 * KS_EVENT_VIEW_FILTER_MASK because we want to find a visible event.
	 */
	req->vis_mask = KS_EVENT_VIEW_FILTER_MASK;

	if (col && col->size)
		entry = kshark_get_collection_entry_front(&req, histo->data,
							  col, index);
	else
		entry = kshark_get_entry_front(req, histo->data, index);

	kshark_free_entry_request(req);

	if (!entry || !entry->visible) {
		/* No visible entry has been found. */
		return false;
	}

	return true;
}

/**
 * @brief Check if a visible trace event from a given Task exists in this bin.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param pid: Process Id of the task.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns True, if a visible entry exists in this bin. Else false.
 */
bool ksmodel_task_visible_event_exist(struct kshark_trace_histo *histo,
				      int bin, int pid,
				      struct kshark_entry_collection *col,
				      ssize_t *index)
{
	struct kshark_entry_request *req;
	const struct kshark_entry *entry;

	if (index)
		*index = KS_EMPTY_BIN;

	/* Set the position at the beginning of the bin and go forward. */
	req = ksmodel_entry_front_request_alloc(histo,
						bin, true,
						kshark_match_pid, pid);
	if (!req)
		return false;

	/*
	 * The default visibility mask of the Model Data request is
	 * KS_GRAPH_VIEW_FILTER_MASK. Change the mask to
	 * KS_EVENT_VIEW_FILTER_MASK because we want to find a visible event.
	 */
	req->vis_mask = KS_EVENT_VIEW_FILTER_MASK;

	if (col && col->size)
		entry = kshark_get_collection_entry_front(&req, histo->data,
							  col, index);
	else
		entry = kshark_get_entry_front(req, histo->data, index);

	kshark_free_entry_request(req);

	if (!entry || !entry->visible) {
		/* No visible entry has been found. */
		return false;
	}

	return true;
}

static bool match_cpu_missed_events(struct kshark_context *kshark_ctx,
				    struct kshark_entry *e, int cpu)
{
	return e->event_id == -EOVERFLOW && e->cpu == cpu;
}

static bool match_pid_missed_events(struct kshark_context *kshark_ctx,
				    struct kshark_entry *e, int pid)
{
	return e->event_id == -EOVERFLOW && e->pid == pid;
}

/**
 * @brief In a given CPU and bin, start from the front end of the bin and go towards
 *	  the back end, searching for a Missed Events entry.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param cpu: CPU Id.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns Pointer ot a kshark_entry, if an entry has been found. Else NULL.
 */
const struct kshark_entry *
ksmodel_get_cpu_missed_events(struct kshark_trace_histo *histo,
			      int bin, int cpu,
			      struct kshark_entry_collection *col,
			      ssize_t *index)
{
	return ksmodel_get_entry_front(histo, bin, true,
				       match_cpu_missed_events, cpu,
				       col, index);
}

/**
 * @brief In a given task and bin, start from the front end of the bin and go towards
 *	  the back end, searching for a Missed Events entry.
 *
 * @param histo: Input location for the model descriptor.
 * @param bin: Bin id.
 * @param pid: Process Id of the task.
 * @param col: Optional input location for Data collection.
 * @param index: Optional output location for the index of the requested
 *		 entry inside the array.
 *
 * @returns Pointer ot a kshark_entry, if an entry has been found. Else NULL.
 */
const struct kshark_entry *
ksmodel_get_task_missed_events(struct kshark_trace_histo *histo,
			       int bin, int pid,
			       struct kshark_entry_collection *col,
			       ssize_t *index)
{
	return ksmodel_get_entry_front(histo, bin, true,
				       match_pid_missed_events, pid,
				       col, index);
}