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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2008-2009, Intel Corporation.
* All Rights Reserved.
*/
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include "latencytop.h"
/* Statistics for each process/thread. */
typedef struct _lt_stat_collection lt_stat_collection_t;
typedef gboolean (*check_child_func_t) (gpointer key,
lt_stat_collection_t *stat, void *user);
typedef struct {
lt_stat_entry_t lt_grp_summary;
/* cause_id -> stat entry */
GHashTable *lt_grp_cidlist;
} lt_datagroup_t;
#define NGROUPS 2
#define GROUP_CAUSE 0
#define GROUP_SOBJ 1
/*
* A data collection hierarchy involving three entities - system, process
* and thread. The hierarchic relationship is as follows :
*
* 1 system -> 1 or more processes -> 1 or more threads
*/
struct _lt_stat_collection {
lt_stat_level_t lt_sc_level;
unsigned int lt_sc_id;
char *lt_sc_name;
lt_datagroup_t lt_sc_groups[NGROUPS];
/*
* The following fields: lt_sc_parent, lt_sc_children and
* lt_sc_check_child_func maintain the tree structure.
*/
lt_stat_collection_t *lt_sc_parent; /* Parent node */
GHashTable *lt_sc_children; /* pid/tid -> lt_stat_collection_t */
check_child_func_t lt_sc_check_child_func; /* Release dead children */
};
/* Internal data structure to back up a stat_list */
typedef struct _lt_stat_list lt_stat_list_t;
typedef void (*free_list_func_t)(lt_stat_list_t *);
struct _lt_stat_list {
int lt_sl_entry_count;
lt_stat_entry_t **lt_sl_entries;
uint64_t lt_sl_gtotal;
free_list_func_t lt_sl_free_func;
};
/* Root of the collection hierarchy: system level statistics */
static lt_stat_collection_t *stat_system = NULL;
/*
* Data structure to hold synchronization objects.
* We don't use normal "cause table" because this needs to be cleared
* every time we refresh in order to make sure that stale synchronization
* objects don't consume memory.
*/
typedef struct {
int lt_soi_type;
unsigned long long lt_soi_addr;
} lt_sobj_id_t;
typedef struct {
lt_sobj_id_t lt_so_oid;
int lt_so_cause_id;
char lt_so_string[32]; /* Enough to hold "%s: 0x%llX" */
} lt_sobj_t;
static GHashTable *sobj_table = NULL;
static int sobj_table_len = 0;
/*
* Lower 32-bit of the address of synchronization objects is used to hash
* them.
*/
static guint
sobj_id_hash(lt_sobj_id_t *id)
{
g_assert(id != NULL);
return (id->lt_soi_addr & 0xFFFFFFFF);
}
/*
* Test if two synchronization objects are the same.
*/
static gboolean
sobj_id_equal(lt_sobj_id_t *a, lt_sobj_id_t *b)
{
g_assert(a != NULL && b != NULL);
return (a->lt_soi_type == b->lt_soi_type &&
a->lt_soi_addr == b->lt_soi_addr);
}
/*
* Look up the cause_id of a synchronization object.
* Note that this cause_id is only unique in GROUP_SOBJ, and changes after
* a refresh.
*/
static lt_sobj_t *
lookup_sobj(lt_sobj_id_t *id)
{
const char *stype_str[] = {
"None",
"Mutex",
"RWLock",
"CV",
"Sema",
"User",
"User_PI",
"Shuttle"
};
const int stype_str_len =
sizeof (stype_str) / sizeof (stype_str[0]);
lt_sobj_t *ret = NULL;
g_assert(id != NULL);
if (id->lt_soi_type < 0 || id->lt_soi_type >= stype_str_len) {
return (NULL);
}
if (sobj_table != NULL) {
ret = (lt_sobj_t *)g_hash_table_lookup(sobj_table, id);
} else {
sobj_table = g_hash_table_new_full(
(GHashFunc)sobj_id_hash, (GEqualFunc)sobj_id_equal,
NULL, (GDestroyNotify)free);
lt_check_null(sobj_table);
}
if (ret == NULL) {
ret = (lt_sobj_t *)lt_zalloc(sizeof (lt_sobj_t));
ret->lt_so_cause_id = ++sobj_table_len;
(void) snprintf(ret->lt_so_string, sizeof (ret->lt_so_string),
"%s: 0x%llX", stype_str[id->lt_soi_type], id->lt_soi_addr);
ret->lt_so_oid.lt_soi_type = id->lt_soi_type;
ret->lt_so_oid.lt_soi_addr = id->lt_soi_addr;
g_hash_table_insert(sobj_table, &ret->lt_so_oid, ret);
}
return (ret);
}
/*
* Check if a process exists by using /proc/pid
*/
/* ARGSUSED */
static gboolean
check_process(gpointer key, lt_stat_collection_t *stat, void *user)
{
char name[PATH_MAX];
(void) snprintf(name, PATH_MAX, "/proc/%u", stat->lt_sc_id);
return (lt_file_exist(name) ? FALSE : TRUE);
}
/*
* Check if a thread exists by using /proc/pid/lwp/tid
*/
/* ARGSUSED */
static gboolean
check_thread(gpointer key, lt_stat_collection_t *stat, void *user)
{
char name[PATH_MAX];
g_assert(stat->lt_sc_parent != NULL);
g_assert(stat->lt_sc_parent->lt_sc_level == LT_LEVEL_PROCESS);
(void) snprintf(name, PATH_MAX, "/proc/%u/lwp/%u",
stat->lt_sc_parent->lt_sc_id, stat->lt_sc_id);
return (lt_file_exist(name) ? FALSE : TRUE);
}
/*
* Helper function to free a stat node.
*/
static void
free_stat(lt_stat_collection_t *stat)
{
int i;
if (stat == NULL) {
return;
}
for (i = 0; i < NGROUPS; ++i) {
if (stat->lt_sc_groups[i].lt_grp_cidlist != NULL) {
g_hash_table_destroy(stat->lt_sc_groups[i].
lt_grp_cidlist);
}
}
if (stat->lt_sc_children != NULL) {
g_hash_table_destroy(stat->lt_sc_children);
}
if (stat->lt_sc_name != NULL) {
free(stat->lt_sc_name);
}
free(stat);
}
/*
* Helper function to initialize a stat node.
*/
/* ARGSUSED */
static void
clear_stat(gpointer key, lt_stat_collection_t *stat, void *user)
{
int i;
g_assert(stat != NULL);
for (i = 0; i < NGROUPS; ++i) {
if (stat->lt_sc_groups[i].lt_grp_cidlist != NULL) {
g_hash_table_destroy(stat->lt_sc_groups[i].
lt_grp_cidlist);
stat->lt_sc_groups[i].lt_grp_cidlist = NULL;
}
stat->lt_sc_groups[i].lt_grp_summary.lt_se_data.lt_s_count = 0;
stat->lt_sc_groups[i].lt_grp_summary.lt_se_data.lt_s_total = 0;
stat->lt_sc_groups[i].lt_grp_summary.lt_se_data.lt_s_max = 0;
}
if (stat->lt_sc_children != NULL) {
g_hash_table_foreach_remove(stat->lt_sc_children,
(GHRFunc)stat->lt_sc_check_child_func, NULL);
g_hash_table_foreach(stat->lt_sc_children,
(GHFunc)clear_stat, NULL);
}
}
/*
* Update a collection with the given value.
* Recursively update parents in the hierarchy until the root is reached.
*/
static void
update_stat_entry(lt_stat_collection_t *stat, int cause_id,
lt_stat_type_t type, uint64_t value,
const char *string, int group_to_use)
{
lt_stat_entry_t *entry = NULL;
lt_datagroup_t *group;
if (group_to_use < 0 || group_to_use >= NGROUPS) {
return;
}
group = &(stat->lt_sc_groups[group_to_use]);
if (group->lt_grp_cidlist != NULL) {
entry = (lt_stat_entry_t *)g_hash_table_lookup(
group->lt_grp_cidlist, LT_INT_TO_POINTER(cause_id));
} else {
group->lt_grp_cidlist = g_hash_table_new_full(
g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify)free);
lt_check_null(group->lt_grp_cidlist);
}
if (entry == NULL) {
entry = (lt_stat_entry_t *)lt_zalloc(sizeof (lt_stat_entry_t));
entry->lt_se_string = string;
switch (group_to_use) {
case GROUP_CAUSE:
entry->lt_se_type = STAT_CAUSE;
entry->lt_se_tsdata.lt_se_t_cause.lt_se_c_id = cause_id;
entry->lt_se_tsdata.lt_se_t_cause.lt_se_c_flags =
lt_table_get_cause_flag(cause_id, CAUSE_ALL_FLAGS);
/* hide the first '#' */
if ((entry->lt_se_tsdata.lt_se_t_cause.lt_se_c_flags
& CAUSE_FLAG_HIDE_IN_SUMMARY) != 0) {
++entry->lt_se_string;
}
break;
case GROUP_SOBJ:
entry->lt_se_type = STAT_SOBJ;
entry->lt_se_tsdata.lt_se_t_sobj.lt_se_s_id = cause_id;
break;
}
g_hash_table_insert(group->lt_grp_cidlist,
LT_INT_TO_POINTER(cause_id), entry);
}
lt_update_stat_value(&entry->lt_se_data, type, value);
if (group_to_use == GROUP_SOBJ ||
(entry->lt_se_tsdata.lt_se_t_cause.lt_se_c_flags &
CAUSE_FLAG_HIDE_IN_SUMMARY) == 0) {
lt_update_stat_value(&group->lt_grp_summary.lt_se_data, type,
value);
}
if (stat->lt_sc_parent != NULL) {
update_stat_entry(stat->lt_sc_parent, cause_id, type, value,
string, group_to_use);
}
}
/*
* Identify the cause of latency from the given stack trace.
* Return cause_id.
*/
static void
find_cause(char *stack, int *cause_id, int *cause_priority)
{
int cause_temp;
int prio_temp;
int cause = INVALID_CAUSE;
int priority = 0;
int found = 0;
g_assert(cause_id != NULL);
g_assert(cause_priority != NULL);
while (stack != NULL) {
char *sep;
sep = strchr(stack, ' ');
if (sep != NULL) {
*sep = '\0';
}
found = lt_table_cause_from_stack(stack, &cause_temp,
&prio_temp);
if (found && (cause == INVALID_CAUSE ||
HIGHER_PRIORITY(prio_temp, priority))) {
cause = cause_temp;
priority = prio_temp;
}
if (sep != NULL) {
*sep = ' ';
stack = sep + 1;
} else {
stack = NULL;
}
}
*cause_id = cause;
*cause_priority = priority;
}
/*
* Create a new collection and hook it to the parent.
*/
static lt_stat_collection_t *
new_collection(lt_stat_level_t level, unsigned int id, char *name,
lt_stat_collection_t *parent, check_child_func_t check_child_func)
{
int i;
lt_stat_collection_t *ret;
ret = (lt_stat_collection_t *)
lt_zalloc(sizeof (lt_stat_collection_t));
ret->lt_sc_level = level;
ret->lt_sc_check_child_func = check_child_func;
ret->lt_sc_id = id;
ret->lt_sc_name = name;
for (i = 0; i < NGROUPS; ++i) {
ret->lt_sc_groups[i].lt_grp_summary.lt_se_string =
(const char *)name;
}
if (parent != NULL) {
ret->lt_sc_parent = parent;
if (parent->lt_sc_children == NULL) {
parent->lt_sc_children = g_hash_table_new_full(
g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify)free_stat);
lt_check_null(parent->lt_sc_children);
}
g_hash_table_insert(parent->lt_sc_children,
LT_INT_TO_POINTER((int)id), ret);
}
return (ret);
}
/*
* Find the "leaf" in the collection hierarchy, using the given pid and tid.
*/
static lt_stat_collection_t *
get_stat_c(pid_t pid, id_t tid)
{
lt_stat_collection_t *stat_p = NULL;
lt_stat_collection_t *stat_t = NULL;
if (stat_system == NULL) {
stat_system = new_collection(LT_LEVEL_GLOBAL,
PID_SYS_GLOBAL, lt_strdup("SYSTEM"), NULL, check_process);
} else if (stat_system->lt_sc_children != NULL) {
stat_p = (lt_stat_collection_t *)
g_hash_table_lookup(stat_system->lt_sc_children,
LT_INT_TO_POINTER(pid));
}
if (stat_p == NULL) {
char *fname;
fname = lt_get_proc_field(pid, LT_FIELD_FNAME);
if (fname == NULL) {
/*
* we could not get the executable name of the
* process; the process is probably already dead.
*/
return (NULL);
}
stat_p = new_collection(LT_LEVEL_PROCESS,
(unsigned int)pid, fname, stat_system, check_thread);
} else if (stat_p->lt_sc_children != NULL) {
stat_t = (lt_stat_collection_t *)
g_hash_table_lookup(stat_p->lt_sc_children,
LT_INT_TO_POINTER(tid));
}
if (stat_t == NULL) {
const int tname_size = 16; /* Enough for "Thread %d" */
char *tname;
tname = (char *)lt_zalloc(tname_size);
(void) snprintf(tname, tname_size, "Thread %d", tid);
stat_t = new_collection(LT_LEVEL_THREAD,
(unsigned int)tid, tname, stat_p, NULL);
}
return (stat_t);
}
/*
* Update statistics with the given cause_id. Values will be added to
* internal statistics.
*/
void
lt_stat_update_cause(pid_t pid, id_t tid, int cause_id, lt_stat_type_t type,
uint64_t value)
{
const char *string;
lt_stat_collection_t *stat_t = NULL;
if (cause_id < 0 || value == 0) {
return;
}
if (lt_table_get_cause_flag(cause_id, CAUSE_FLAG_DISABLED)) {
/* Ignore this cause */
return;
}
stat_t = get_stat_c(pid, tid);
if (stat_t == NULL) {
/* Process must be dead. */
return;
}
string = lt_table_get_cause_name(cause_id);
update_stat_entry(stat_t, cause_id, type, value, string, GROUP_CAUSE);
}
/*
* Update statistics with the given stack trace.
* The stack trace is mapped to a cause and lt_stat_update_cause() is called
* to update statistics.
*/
void
lt_stat_update(pid_t pid, id_t tid, char *stack, char *tag,
unsigned int tag_priority, lt_stat_type_t type, uint64_t value)
{
int tag_cause_id = INVALID_CAUSE;
int stack_cause_id = INVALID_CAUSE;
int cause_id = INVALID_CAUSE;
int stack_priority = 0;
if (value == 0) {
return;
}
find_cause(stack, &stack_cause_id, &stack_priority);
if (tag_priority != 0) {
tag_cause_id = lt_table_cause_from_name(tag, 0, 0);
if (tag_cause_id == INVALID_CAUSE) {
/* This must be a syscall tag */
char tmp[64];
(void) snprintf(tmp, sizeof (tmp), "Syscall: %s", tag);
tag_cause_id = lt_table_cause_from_name(tmp, 1, 0);
}
}
cause_id = (tag_priority > stack_priority) ? tag_cause_id :
stack_cause_id;
if (cause_id == INVALID_CAUSE) {
/*
* We got an unmapped stack. Set SPECIAL flag to display it
* in pane 2. This makes it easier to find the cause.
*/
cause_id = lt_table_cause_from_name(stack, 1,
CAUSE_FLAG_SPECIAL);
lt_klog_log(LT_KLOG_LEVEL_UNMAPPED, pid, stack, type, value);
} else {
lt_klog_log(LT_KLOG_LEVEL_MAPPED, pid, stack, type, value);
}
lt_stat_update_cause(pid, tid, cause_id, type, value);
}
/*
* Zero out all statistics, but keep the data structures in memory
* to be used to hold new data immediately following.
*/
void
lt_stat_clear_all(void)
{
if (stat_system != NULL) {
clear_stat(NULL, stat_system, NULL);
}
if (sobj_table != NULL) {
g_hash_table_destroy(sobj_table);
sobj_table = NULL;
}
}
/*
* Clean up function that frees all memory used for statistics.
*/
void
lt_stat_free_all(void)
{
if (stat_system != NULL) {
free_stat(stat_system);
stat_system = NULL;
}
if (sobj_table != NULL) {
g_hash_table_destroy(sobj_table);
sobj_table = NULL;
}
}
/*
* Get top N causes of latency for a process. Return handle to a stat_list.
* Use pid = PID_SYS_GLOBAL to get global top list.
* Call lt_stat_list_free after use to clean up.
*/
void *
lt_stat_list_create(lt_list_type_t list_type, lt_stat_level_t level,
pid_t pid, id_t tid, int count, lt_sort_t sort_by)
{
GCompareFunc func;
GList *list, *walk;
lt_stat_collection_t *stat_c = NULL;
lt_stat_list_t *ret;
lt_datagroup_t *group;
if (level == LT_LEVEL_GLOBAL) {
/* Use global entry */
stat_c = stat_system;
} else if (stat_system != NULL && stat_system->lt_sc_children != NULL) {
/* Find process entry first */
stat_c = (lt_stat_collection_t *)g_hash_table_lookup(
stat_system->lt_sc_children, LT_INT_TO_POINTER(pid));
if (level == LT_LEVEL_THREAD) {
/*
* If thread entry is requested, find it based on
* process entry.
*/
if (stat_c != NULL && stat_c->lt_sc_children != NULL) {
stat_c = (lt_stat_collection_t *)
g_hash_table_lookup(stat_c->lt_sc_children,
LT_INT_TO_POINTER(tid));
} else {
/*
* Thread entry was not found; set it to NULL,
* so that we can return empty list later.
*/
stat_c = NULL;
}
}
}
ret = (lt_stat_list_t *)lt_zalloc(sizeof (lt_stat_list_t));
ret->lt_sl_entries = (lt_stat_entry_t **)
lt_zalloc(count * sizeof (lt_stat_entry_t *));
if (stat_c == NULL) {
/* Empty list */
return (ret);
}
if (list_type == LT_LIST_SOBJ) {
group = &(stat_c->lt_sc_groups[GROUP_SOBJ]);
} else {
group = &(stat_c->lt_sc_groups[GROUP_CAUSE]);
}
if (group->lt_grp_cidlist == NULL) {
/* Empty list */
return (ret);
}
ret->lt_sl_gtotal = group->lt_grp_summary.lt_se_data.lt_s_total;
list = g_hash_table_get_values(group->lt_grp_cidlist);
switch (sort_by) {
case LT_SORT_TOTAL:
func = (GCompareFunc)lt_sort_by_total_desc;
break;
case LT_SORT_MAX:
func = (GCompareFunc)lt_sort_by_max_desc;
break;
case LT_SORT_AVG:
func = (GCompareFunc)lt_sort_by_avg_desc;
break;
case LT_SORT_COUNT:
func = (GCompareFunc)lt_sort_by_count_desc;
break;
}
list = g_list_sort(list, func);
for (walk = list;
walk != NULL && count > 0;
walk = g_list_next(walk), --count) {
lt_stat_entry_t *data = (lt_stat_entry_t *)walk->data;
if (list_type == LT_LIST_CAUSE &&
data->lt_se_type == STAT_CAUSE &&
(data->lt_se_tsdata.lt_se_t_cause.lt_se_c_flags &
CAUSE_FLAG_HIDE_IN_SUMMARY) != 0) {
continue;
}
if (list_type == LT_LIST_SPECIALS &&
data->lt_se_type == STAT_CAUSE &&
(data->lt_se_tsdata.lt_se_t_cause.lt_se_c_flags &
CAUSE_FLAG_SPECIAL) == 0) {
continue;
}
if (data->lt_se_data.lt_s_count == 0) {
break;
}
ret->lt_sl_entries[ret->lt_sl_entry_count++] = data;
}
g_list_free(list);
return (ret);
}
/*
* Free memory allocated by lt_stat_list_create().
*/
void
lt_stat_list_free(void *ptr)
{
lt_stat_list_t *list = (lt_stat_list_t *)ptr;
if (list == NULL) {
return;
}
if (list->lt_sl_free_func != NULL) {
list->lt_sl_free_func(list);
}
if (list->lt_sl_entries != NULL) {
free(list->lt_sl_entries);
}
free(list);
}
/*
* Check if the given list contains the given item.
*/
int
lt_stat_list_has_item(void *ptr, int i)
{
lt_stat_list_t *list = (lt_stat_list_t *)ptr;
if (list == NULL || i < 0 || i >= list->lt_sl_entry_count ||
list->lt_sl_entries[i] == NULL) {
return (0);
}
return (1);
}
/*
* Get display name of the given item i in the given list.
*/
const char *
lt_stat_list_get_reason(void *ptr, int i)
{
lt_stat_list_t *list = (lt_stat_list_t *)ptr;
if (list == NULL || i < 0 || i >= list->lt_sl_entry_count ||
list->lt_sl_entries[i] == NULL) {
return (NULL);
}
g_assert(list->lt_sl_entries[i]->lt_se_string != NULL);
return (list->lt_sl_entries[i]->lt_se_string);
}
/*
* Get maximum value of the given item i in the given list.
*/
uint64_t
lt_stat_list_get_max(void *ptr, int i)
{
lt_stat_list_t *list = (lt_stat_list_t *)ptr;
if (list == NULL || i < 0 || i >= list->lt_sl_entry_count ||
list->lt_sl_entries[i] == NULL) {
return (0);
}
return (list->lt_sl_entries[i]->lt_se_data.lt_s_max);
}
/*
* Get total value of the given item i in the given list.
*/
uint64_t
lt_stat_list_get_sum(void *ptr, int i)
{
lt_stat_list_t *list = (lt_stat_list_t *)ptr;
if (list == NULL || i < 0 || i >= list->lt_sl_entry_count ||
list->lt_sl_entries[i] == NULL) {
return (0);
}
return (list->lt_sl_entries[i]->lt_se_data.lt_s_total);
}
/*
* Get count value of the given item i in the given list.
*/
uint64_t
lt_stat_list_get_count(void *ptr, int i)
{
lt_stat_list_t *list = (lt_stat_list_t *)ptr;
if (list == NULL || i < 0 || i >= list->lt_sl_entry_count ||
list->lt_sl_entries[i] == NULL) {
return (0);
}
return (list->lt_sl_entries[i]->lt_se_data.lt_s_count);
}
/*
* Get grand total of all latency in the list.
*/
uint64_t
lt_stat_list_get_gtotal(void *ptr)
{
lt_stat_list_t *list = (lt_stat_list_t *)ptr;
if (list == NULL) {
return (0);
}
return (list->lt_sl_gtotal);
}
/*
* ============================================================================
* Process and thread list.
* They share a lot of the static variables that are used for keeping
* statistics, hence they are located in this file.
*/
/*
* Helper function, sort by PID/TID ascend.
*/
static int
sort_id(lt_stat_collection_t *a, lt_stat_collection_t *b)
{
return ((int)(a->lt_sc_id - b->lt_sc_id));
}
/*
* Get the current list of processes. Call lt_stat_proc_list_free after use
* to clean up.
*/
static int
plist_create(pid_t ** list)
{
GList *pid_list, *walk;
int ret, count;
ret = g_hash_table_size(stat_system->lt_sc_children);
*list = (pid_t *)lt_malloc(sizeof (pid_t) * ret);
pid_list = g_hash_table_get_values(stat_system->lt_sc_children);
pid_list = g_list_sort(pid_list, (GCompareFunc)sort_id);
for (walk = pid_list, count = 0;
walk != NULL && count < ret;
walk = g_list_next(walk), ++count) {
(*list)[count] = (int)
((lt_stat_collection_t *)(walk->data))->lt_sc_id;
}
g_list_free(pid_list);
return (ret);
}
/*
* Count the no. of threads currently present in a process.
* Only thread that have SSLEEP are counted.
*/
/* ARGSUSED */
static void
count_threads(gpointer key, lt_stat_collection_t *stat_c, int *ret)
{
g_assert(ret != NULL);
if (stat_c->lt_sc_children != NULL) {
*ret += g_hash_table_size(stat_c->lt_sc_children);
}
}
/*
* Get current list of processes and threads.
* Call lt_stat_proc_list_free after use to clean up.
*/
static int
tlist_create(pid_t ** plist, id_t ** tlist)
{
GList *pid_list, *walk_p;
GList *tid_list, *walk_t;
int ret = 0;
int count = 0;
g_hash_table_foreach(stat_system->lt_sc_children,
(GHFunc)count_threads, &ret);
*plist = (pid_t *)lt_malloc(sizeof (pid_t) * ret);
*tlist = (id_t *)lt_malloc(sizeof (id_t) * ret);
pid_list = g_hash_table_get_values(stat_system->lt_sc_children);
pid_list = g_list_sort(pid_list, (GCompareFunc)sort_id);
for (walk_p = pid_list; walk_p != NULL;
walk_p = g_list_next(walk_p)) {
lt_stat_collection_t *stat_p =
(lt_stat_collection_t *)walk_p->data;
if (stat_p->lt_sc_children == NULL) {
continue;
}
tid_list = g_hash_table_get_values(stat_p->lt_sc_children);
tid_list = g_list_sort(tid_list, (GCompareFunc)sort_id);
for (walk_t = tid_list; walk_t != NULL;
walk_t = g_list_next(walk_t)) {
lt_stat_collection_t *stat_t =
(lt_stat_collection_t *)walk_t->data;
(*plist)[count] = (int)stat_p->lt_sc_id;
(*tlist)[count] = (int)stat_t->lt_sc_id;
++count;
}
g_list_free(tid_list);
}
g_list_free(pid_list);
g_assert(count == ret);
return (ret);
}
/*
* List of processes that are tracked by LatencyTOP.
*/
int
lt_stat_proc_list_create(pid_t ** plist, id_t ** tlist)
{
if (plist == NULL) {
return (-1);
}
if (stat_system == NULL || stat_system->lt_sc_children == NULL) {
*plist = NULL;
if (tlist != NULL) {
*tlist = NULL;
}
return (0);
}
if (tlist == NULL) {
return (plist_create(plist));
} else {
return (tlist_create(plist, tlist));
}
}
/*
* Free memory allocated by lt_stat_proc_list_create().
*/
void
lt_stat_proc_list_free(pid_t *plist, id_t *tlist)
{
if (plist != NULL) {
free(plist);
}
if (tlist != NULL) {
free(tlist);
}
}
/*
* Get executable name of the given process (ID).
*/
const char *
lt_stat_proc_get_name(pid_t pid)
{
lt_stat_collection_t *stat_p = NULL;
if (stat_system == NULL || stat_system->lt_sc_children == NULL) {
return (NULL);
}
stat_p = (lt_stat_collection_t *)g_hash_table_lookup(
stat_system->lt_sc_children, LT_INT_TO_POINTER(pid));
if (stat_p != NULL) {
return (stat_p->lt_sc_name);
} else {
return (NULL);
}
}
/*
* Get number of threads.
*/
int
lt_stat_proc_get_nthreads(pid_t pid)
{
lt_stat_collection_t *stat_p = NULL;
if (stat_system == NULL || stat_system->lt_sc_children == NULL) {
return (0);
}
stat_p = (lt_stat_collection_t *)g_hash_table_lookup(
stat_system->lt_sc_children, LT_INT_TO_POINTER(pid));
if (stat_p != NULL) {
return (g_hash_table_size(stat_p->lt_sc_children));
} else {
return (0);
}
}
/*
* Update statistics for synchronization objects.
*/
void
lt_stat_update_sobj(pid_t pid, id_t tid, int stype,
unsigned long long wchan,
lt_stat_type_t type, uint64_t value)
{
lt_sobj_id_t id;
lt_sobj_t *sobj;
int cause_id;
lt_stat_collection_t *stat_t = NULL;
stat_t = get_stat_c(pid, tid);
if (stat_t == NULL) {
return;
}
id.lt_soi_type = stype;
id.lt_soi_addr = wchan;
sobj = lookup_sobj(&id);
if (sobj == NULL) {
return;
}
cause_id = sobj->lt_so_cause_id;
update_stat_entry(stat_t, cause_id, type, value,
sobj->lt_so_string, GROUP_SOBJ);
}
|