-
Notifications
You must be signed in to change notification settings - Fork 10
/
cgget.c
4131 lines (3756 loc) · 109 KB
/
cgget.c
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
/*
* cgget.c - Display the parameters of cgroup.
*
* Copyright (C) 2012 FUJITSU LIMITED
* Author: Zhang Xiaohe <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "defs.h"
#include <getopt.h>
/* HZ is 1000 as default after kernel 2.6 */
#ifdef HZ
#undef HZ
#define HZ 1000
#endif
/* USER_HZ is 100 only on X86 platform */
#define USER_HZ 100
#define NSEC_PER_SEC 1000000000ULL
#define NSEC_PER_USEC 1000L
#define DEV_BLOCK 1
#define DEV_CHAR 2
#define DEV_ALL 4
#define MAJMINLEN 13
#define ACCLEN 4
#define ACC_MKNOD 1
#define ACC_READ 2
#define ACC_WRITE 4
#define ACC_MASK (ACC_MKNOD|ACC_READ|ACC_WRITE)
#define LRU_BASE 0
#define LRU_ACTIVE 1
#define LRU_FILE 2
#define CGROUP_HIER_MAX 100
#define CGROUP_STR_LEN 32
#define MODE_SEPARATE_PATH 0x01
#define MODE_COMBINE_PATH 0x02
#define CGGET_MEMBER_OFFSET_INIT(TABLE, MEMBER, STRUCT, X) \
TABLE.MEMBER = MEMBER_OFFSET(STRUCT, X)
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_LONG)
#define BLKIO_MERGE_POL(x, val) (((x) << 16) | (val))
#define MINORBITS 20
#define MINORMASK ((1U << MINORBITS) - 1)
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
#define CGROUP_SUBSYS_COUNT cgroup_subsys_num
#define CGROUP_NOT_SUPPORT 0
#define CGROUP_SUPPORTED 1
#define for_each_possible_cpu(cpu) \
for ((cpu) = -1; (cpu) = next_possible_cpu(cpu), \
(cpu) < kt->cpus;)
struct cgroup_spec {
char subsys_str[CGROUP_STR_LEN];
char path[FILENAME_MAX];
};
struct cgroupfs_root_offset_table {
long cgroupfs_root_top_cgroup;
long cgroupfs_root_number_of_cgroups;
long cgroupfs_root_root_list;
};
struct cgroup_offset_table {
long cgroup_sibling;
long cgroup_children;
long cgroup_parent;
long cgroup_dentry;
long cgroup_subsys;
};
struct cpuset_offset_table {
long cpuset_flags;
long cpuset_cpus_allowed;
long cpuset_mems_allowed;
long cpuset_fmeter;
long cpuset_shed_relax_domain_level;
};
struct tg_offset_table {
long tg_shares;
long tg_rt_bandwidth;
long tg_cfs_bandwidth;
};
struct cpuacct_offset_table {
long cpuacct_cpuusage;
long cpuacct_cpustat;
};
struct hugetlb_offset_table {
long hugetlb_hugepage;
};
struct memory_offset_table {
long memory_res;
long memory_memsw;
long memory_kmem;
long memory_tcp_mem;
long memory_info;
long memory_stat;
long memory_oom_kill_disable;
long memory_under_oom;
long memory_mcai;
long memory_swappiness;
long memory_use_hierarchy;
long counter_usage;
long counter_max_usage;
long counter_limit;
long counter_soft_limit;
long counter_failcnt;
long perzone_count;
};
struct devices_offset_table {
long devices_whitelist;
long devices_behavior;
long item_major;
long item_minor;
long item_type;
long item_access;
long item_list;
};
struct freezer_offset_table {
long freezer_state;
};
struct cls_offset_table {
long cls_classid;
};
struct blkio_offset_table {
long blkio_blkg_list;
long blkio_policy_list;
long blkio_weight;
long blkg_blkcg_node;
long blkg_dev;
long blkg_plid;
long blkg_stats;
long blkg_stats_cpu;
long blkp_dev;
long blkp_plid;
long blkp_fileid;
long blkp_weight;
long blkg_pd;
long cfq_group_stats;
long cfqg_stats_service_bytes;
long cfqg_stats_serviced;
long cfqg_stats_time;
long cfqg_stats_sectors;
long cfqg_stats_service_time;
long cfqg_stats_wait_time;
long cfqg_stats_merged;
long cfqg_stats_queued;
};
struct netprio_offset_table {
long netprio_prioidx;
};
static struct cgroupfs_root_offset_table cgroupfs_root_offset_table = {0};
static struct cgroup_offset_table cgroup_offset_table = {0};
static struct cpuset_offset_table cpuset_offset_table = {0};
static struct tg_offset_table tg_offset_table = {0};
static struct cpuacct_offset_table cpuacct_offset_table = {0};
static struct hugetlb_offset_table hugetlb_offset_table = {0};
static struct memory_offset_table memory_offset_table = {0};
static struct devices_offset_table devices_offset_table = {0};
static struct freezer_offset_table freezer_offset_table = {0};
static struct cls_offset_table cls_offset_table = {0};
static struct blkio_offset_table blkio_offset_table = {0};
static struct netprio_offset_table netprio_offset_table = {0};
static const char *cpuset_params[] = {
"cpu_exclusive",
"mem_exclusive",
"mem_hardwall",
"memory_migrate",
"sched_load_balance",
"memory_spread_page",
"memory_spread_slab",
"memory_pressure_enabled",
"memory_pressure",
"sched_relax_domain_level",
"mems",
"cpus"
};
enum {
CS_CPU_EXCLUSIVE,
CS_MEM_EXCLUSIVE,
CS_MEM_HARDWALL,
CS_MEMORY_MIGRATE,
CS_SCHED_LOAD_BALANCE,
CS_SPREAD_PAGE,
CS_SPREAD_SLAB,
CS_MEM_PRESSURE_ENABLE,
CS_MEM_PRESSURE,
CS_SHED_RELAX_DOMAIN_LEVEL,
CS_MEMS,
CS_CPUS,
};
static const char *cpu_params[] = {
"rt_period_us",
"rt_runtime_us",
"stat",
"cfs_period_us",
"cfs_quota_us",
"shares",
};
enum cpu_param_id {
CPU_RT_PERIOD,
CPU_RT_RUNTIME,
CPU_STAT,
CPU_CFS_PERIOD,
CPU_CFS_QUOTA,
CPU_SHARES,
CPU_NR_PARAMS,
};
static const char *cpuacct_params[] = {
"stat",
"usage_percpu",
"usage",
};
enum cpuacct_param_id {
CPUACCT_STAT,
CPUACCT_USAGE_PERCPU,
CPUACCT_USAGE,
CPUACCT_NR_PARAMS,
};
enum memory_param_id {
MEM_TMEM_FAILCNT,
MEM_TMEM_LIMIT,
MEM_TMEM_MAX_USAGE,
MEM_TMEM_USAGE,
MEM_KMEM_FAILCNT,
MEM_KMEM_LIMIT,
MEM_KMEM_MAX_USAGE,
MEM_KMEM_USAGE,
MEM_MEMSW_FAILCNT,
MEM_MEMSW_LIMIT,
MEM_MEMSW_MAX_USAGE,
MEM_MEMSW_USAGE,
MEM_NUMA_STAT,
MEM_OOM_CTRL,
MEM_MCAI,
MEM_SWAP,
MEM_USE_HIER,
MEM_FORCE_EMPTY,
MEM_STAT,
MEM_FAILCNT,
MEM_SOFT_LIMIT,
MEM_LIMIT,
MEM_MAX_USAGE,
MEM_USAGE,
MEM_NR_PARAMS,
};
static const char *memory_params[] = {
"kmem.tcp.failcnt",
"kmem.tcp.limit_in_bytes",
"kmem.tcp.max_usage_in_bytes",
"kmem.tcp.usage_in_bytes",
"kmem.failcnt",
"kmem.limit_in_bytes",
"kmem.max_usage_in_bytes",
"kmem.usage_in_bytes",
"memsw.failcnt",
"memsw.limit_in_bytes",
"memsw.max_usage_in_bytes",
"memsw.usage_in_bytes",
"numa_stat",
"oom_control",
"move_charge_at_immigrate",
"swappiness",
"use_hierarchy",
"force_empty",
"stat",
"failcnt",
"soft_limit_in_bytes",
"limit_in_bytes",
"max_usage_in_bytes",
"usage_in_bytes",
NULL,
};
enum lru_list {
LRU_INACTIVE_ANON = LRU_BASE,
LRU_ACTIVE_ANON = LRU_BASE + LRU_ACTIVE,
LRU_INACTIVE_FILE = LRU_BASE + LRU_FILE,
LRU_ACTIVE_FILE = LRU_BASE + LRU_FILE + LRU_ACTIVE,
LRU_UNEVICTABLE,
NR_LRU_LISTS
};
enum {
MCS_CACHE,
MCS_RSS,
MCS_FILE_MAPPED,
MCS_SWAP,
MCS_PGPGIN,
MCS_PGPGOUT,
MCS_PGFAULT,
MCS_PGMAJFAULT,
MCS_INACTIVE_ANON,
MCS_ACTIVE_ANON,
MCS_INACTIVE_FILE,
MCS_ACTIVE_FILE,
MCS_UNEVICTABLE,
NR_MCS_STAT,
};
struct {
char *local_name;
char *total_name;
} memcg_stat_strings[NR_MCS_STAT] = {
{"cache", "total_cache"},
{"rss", "total_rss"},
{"mapped_file", "total_mapped_file"},
{"swap", "total_swap"},
{"pgpgin", "total_pgpgin"},
{"pgpgout", "total_pgpgout"},
{"pgfault", "total_pgfault"},
{"pgmajfault", "total_pgmajfault"},
{"inactive_anon", "total_inactive_anon"},
{"active_anon", "total_active_anon"},
{"inactive_file", "total_inactive_file"},
{"active_file", "total_active_file"},
{"unevictable", "total_unevictable"}
};
enum freezer_state {
CGROUP_THAWED = 0,
CGROUP_FREEZING,
CGROUP_FROZEN,
};
static const char *freezer_state_strs[] = {
"THAWED",
"FREEZING",
"FROZEN",
};
static const char *blkio_prop_strs[] = {
"weight",
"weight_device",
"io_service_bytes",
"io_serviced",
"time",
"sectors",
"io_service_time",
"io_wait_time",
"io_merged",
"io_queued",
"reset_stats",
};
static const char *blkio_thro_strs[] = {
"throttle.read_bps_device",
"throttle.write_bps_device",
"throttle.read_iops_device",
"throttle.write_iops_device",
"throttle.io_service_bytes",
"throttle.io_serviced",
};
enum blkio_plid {
BLKIO_POLICY_PROP = 0, /* Proportional Bandwidth division */
BLKIO_POLICY_THROTL, /* Throttling */
BLKCG_POLICY_THROTL, /* Id of throtl is 0 from v3.5 */
BLKCG_POLICY_PROP,
};
/* blkio attributes owned by proportional weight policy */
enum blkcg_file_name_prop {
BLKIO_PROP_weight = 1,
BLKIO_PROP_weight_device,
BLKIO_PROP_io_service_bytes,
BLKIO_PROP_io_serviced,
BLKIO_PROP_time,
BLKIO_PROP_sectors,
BLKIO_PROP_io_service_time,
BLKIO_PROP_io_wait_time,
BLKIO_PROP_io_merged,
BLKIO_PROP_io_queued,
BLKIO_PROP_avg_queue_size,
BLKIO_PROP_group_wait_time,
BLKIO_PROP_idle_time,
BLKIO_PROP_empty_time,
BLKIO_PROP_dequeue,
};
enum stat_type {
BLKIO_STAT_SERVICE_TIME,
BLKIO_STAT_SERVICE_BYTES,
BLKIO_STAT_SERVICED,
BLKIO_STAT_WAIT_TIME,
BLKIO_STAT_MERGED,
BLKIO_STAT_QUEUED,
BLKIO_STAT_TIME,
BLKIO_STAT_SECTORS,
};
/* blkio attributes owned by throttle policy */
enum blkcg_file_name_throtl {
BLKIO_THROTL_read_bps_device,
BLKIO_THROTL_write_bps_device,
BLKIO_THROTL_read_iops_device,
BLKIO_THROTL_write_iops_device,
BLKIO_THROTL_io_service_bytes,
BLKIO_THROTL_io_serviced,
};
/* Per cpu stats, added from kernel version 3.0 */
enum stat_type_cpu {
BLKIO_STAT_CPU_SECTORS,
BLKIO_STAT_CPU_SERVICE_BYTES,
BLKIO_STAT_CPU_SERVICED,
BLKIO_STAT_CPU_MERGED,
BLKIO_STAT_CPU_NR
};
enum stat_sub_type {
BLKIO_STAT_READ = 0,
BLKIO_STAT_WRITE,
BLKIO_STAT_SYNC,
BLKIO_STAT_ASYNC,
BLKIO_STAT_TOTAL
};
enum all_subsys_id {
cpuset_subsys_id,
debug_subsys_id,
ns_subsys_id,
cpu_cgroup_subsys_id,
cpuacct_subsys_id,
hugetlb_subsys_id,
mem_cgroup_subsys_id,
devices_subsys_id,
freezer_subsys_id,
net_cls_subsys_id,
blkio_subsys_id,
perf_subsys_id,
net_prio_subsys_id,
CGROUP_SUBSYS_MAX
};
static const char *subsys_name[] = {
"cpuset",
"debug",
"ns",
"cpu",
"cpuacct",
"hugetlb",
"memory",
"devices",
"freezer",
"net_cls",
"blkio",
"perf_event",
"net_prio"
};
struct cgroup_subsys_table {
int subsys_id;
char subsys_str[CGROUP_STR_LEN];
};
static struct cgroup_subsys_table cgroup_subsys_table[CGROUP_SUBSYS_MAX];
static int cgroup_subsys_num = 0;
static int is_cgroup_supported = 0;
static uint variable_flag = 0;
static int var_num[CGROUP_SUBSYS_MAX] = {0};
static char *variable_str[CGROUP_SUBSYS_MAX][30] = {{0}};
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"group", required_argument, 0, 'g'},
{"all", no_argument, 0, 'a'},
{0, 0, 0, 0}
};
int _init(void);
int _fini(void);
char *help_cgget[];
void cmd_cgget(void);
/* printing functions for every subsys */
static void print_cpuset(struct cgroup_spec *, int, ulong);
static void print_cpu(struct cgroup_spec *, int, ulong);
static void print_cpuacct(struct cgroup_spec *, int, ulong);
static void print_hugetlb(struct cgroup_spec *, int, ulong);
static void print_memory(struct cgroup_spec *, int, ulong);
static void print_devices(struct cgroup_spec *, int, ulong);
static void print_freezer(struct cgroup_spec *, int, ulong);
static void print_net_cls(struct cgroup_spec *, int, ulong);
static void print_blkio(struct cgroup_spec *, int, ulong);
static void print_net_prio(struct cgroup_spec *, int, ulong);
#if 0
/* ns and perf_event has nothing to print for now*/
static void print_ns(struct cgroup_spec *, int, ulong);
static void print_perf(struct cgroup_spec *, int, ulong);
#endif
/* offset table initialization functions */
static void cgget_offset_table_init();
static void cgroupfs_root_offset_table_init();
static void cgroup_offset_table_init();
static void cpuset_offset_table_init();
static void tg_offset_table_init();
static void cpuacct_offset_table_init();
static void hugetlb_offset_table_init();
static void memory_offset_table_init();
static void devices_offset_table_init();
static void freezer_offset_table_init();
static void cls_offset_table_init();
static void blkio_offset_table_init();
static void netprio_offset_table_init();
/* printing-assisted functions */
static int read_whitelist(struct cgroup_spec *, ulong, int);
static char *hugepage_fmt(char *, uint64_t);
static inline int is_root_mem_cgroup(ulong);
static uint64_t read_res_counter(ulong, long, char *);
static uint64_t mem_cgroup_read_local_zonestat(ulong, int);
static int64_t mem_cgroup_read_stat(void*, int);
static int get_mem_local_stats(ulong, int64_t *, int);
static int get_mem_total_stats(ulong, int64_t *, int);
static void get_mem_hierarchical_limit(ulong, uint64_t *, uint64_t *, int);
static int read_blkcg_stat(ulong, enum stat_type, int, int, int, char *);
static int read_policy_node(ulong, int, int, char *);
static int blkio_read_map(ulong, int, int, char *);
static uint64_t blkio_get_stat_cpu(ulong, enum stat_type, uint32_t, char *);
static uint64_t blkio_get_stat(ulong, enum stat_type, uint32_t, char *);
static void cpuacct_print_stat(ulong);
static uint64_t cpuacct_print_usage_percpu(ulong);
static void cpu_print_stat(ulong);
static void cpu_print_bandwidth(ulong, long, int, char *);
static ulong get_mz_lru_val(ulong, int, int, int);
static int64_t get_value_acc_ver(int64_t *, int, int, int);
static void get_mem_percpu_stats(ulong, void *);
static int css_member(ulong, char *, int);
static ulong idr_get_next(ulong, int *);
static ulong mem_cgroup_iter(ulong, ulong);
static uint64_t get_mem_usage(ulong, int);
static void mem_print_oom_ctrl(ulong);
static void mem_print_swap(ulong, char *);
static void mem_print_numa_stat(ulong, uint64_t *);
static uint64_t print_u64_rw(char *, uint64_t *);
static uint64_t print_thro_u64_rw(char *, ulong, int);
static void dev_name(ulong, char *);
static int test_policy(ulong, int);
static uint64_t print_cfq_group(ulong, int, long, size_t);
static uint64_t print_throtl_grp(ulong, int, long, size_t);
static int read_policy_group(ulong, int, int, char *);
static void blkio_read_each_blkg_for35(ulong, int, int, char *);
static void blkio_read_each_blkg_for37(ulong, int, int, int, char *);
static void blkio_read_each_blkg_for300(ulong, int, int, int, int, char *);
static ulong read_blkcg_stat_old(ulong, int, char *);
static void blkio_print_param_old(ulong);
static int blkio_print_param_no_group(ulong, int, int, char *);
/* general purpose functions */
static inline int next_possible_cpu(int);
static struct list_head *list_next(void *, void *, long);
static inline int test_bit(int, ulong);
static inline int bitmap_scnlistprintf(char *, unsigned, ulong *, int);
static inline uint64_t jiffies_64_to_clock_t(uint64_t);
static inline int check_endian();
static inline uint64_t ktime_to_ns(ulong);
static inline int bitstr_edit(char *, int, int, int);
static inline void set_majmin(char *, unsigned);
static inline void set_access(char *, short);
static inline char type_to_char(short);
static ulong get_subsys_parent(ulong, int);
static inline char *get_dentry_path(ulong, char *, int);
static void format_path_str(const char *, char *);
static void filter_str(char **, int *);
static int make_cgroup_spec(struct cgroup_spec **, char **,
char **, int *, int*);
static int make_all_cgroup_spec(struct cgroup_spec **, char **,
char **, int *, int *);
static void cgroup_subsys_table_init();
static int parse_variable_spec(char *, char **, int *);
static void print_specified_param(int);
static void print_cgroup_list(char *, struct cgroup_spec **, int, int);
static ulong retrieve_path(ulong, ulong, ulong *, const char *);
static ulong get_css_addr(struct cgroup_spec *, int, ulong);
static int get_subsys_id(struct cgroup_spec *);
static void print_cgroup(char *, struct cgroup_spec *, int, ulong, int);
static int64_t read_member_64(ulong, char *);
static int32_t read_member_32(ulong, char *);
static int fls(int);
static ulong get_subsys_parent(ulong, int);
static uint64_t print_u64(char *, uint64_t);
static void format_path_str(const char *, char *);
static struct command_table_entry command_table[] = {
{"cgget", cmd_cgget, help_cgget, 0},
{NULL}
};
char *help_cgget[] = {
"cgget", /* command name */
"display parameters of cgroup.",
"[-a] [-r <name>] [-g <controller>] <path> ...\n"
" or\n"
" cgget [-a] [-r <name>] -g <controller>:<path> ...",
"Displays the parameter(s) of input cgroup(s).\n"
"If no controller is specified, the values of "
"all possible variables are printed.\n"
"Either command line style is OK, but these can not be mixed.\n",
"-a, --all",
"print the variables for all controllers which consist in the given path.\n",
"-r <name>",
"defines parameter to display.",
"This option can be used multiple times.\n",
"-g <controller>",
"defines controllers whose values should be displayed.",
"This option can be used multiple times.\n",
"-g <controller>:<path>",
"defines control groups whose values should be displayed.",
"This option can be used multiple times.\n",
"-h, --help",
"display this message.\n",
"EXAMPLES",
"1. display the controller 'cpu' in path '/'",
" crash> cgget -g cpu:/",
" /:",
" cpu.rt_period_us: 1000000",
" cpu.rt_runtime_us: 950000",
" cpu.stat: nr_periods 0",
" \tnr_throttled 0",
" \tthrottled_time 0",
" cpu.cfs_period_us: 0",
" cpu.cfs_quota_us: 0",
" cpu.shares: 1024",
" or",
" crash> cgget -g cpu /",
" /:",
" cpu.rt_period_us: 1000000",
" cpu.rt_runtime_us: 950000",
" cpu.stat: nr_periods 0",
" \tnr_throttled 0",
" \tthrottled_time 0",
" cpu.cfs_period_us: 0",
" cpu.cfs_quota_us: 0",
" cpu.shares: 1024",
"2. display the parameter 'cpuset.mems' in path '/libvirt'",
" crash> cgget -r cpuset.mems /libvirt",
" /libvirt:",
" cpuset.mems: 0",
"3. display the controller 'cpu' and paramter 'cpuset.mems' at same time",
" crash> cgget -r cpuset.mems -g cpu /",
" /:",
" cpuset.mems: 0",
" /:",
" cpu.rt_period_us: 1000000",
" cpu.rt_runtime_us: 950000",
" cpu.stat: nr_periods 0",
" \tnr_throttled 0",
" \tthrottled_time 0",
" cpu.cfs_period_us: 0",
" cpu.cfs_quota_us: 0",
" cpu.shares: 1024",
NULL
};
int
_init(void)
{
cgroup_subsys_table_init();
cgget_offset_table_init();
register_extension(command_table);
return 1;
}
/*
* The _fini() function is called if the shared object is unloaded.
* If desired, perform any cleanups here.
*/
int
_fini(void)
{
return 1;
}
static inline int
next_possible_cpu(int cpu)
{
ulong p, mask_addr, mask[BITS_TO_LONGS(kt->cpus)];
if (symbol_exists("cpu_possible_mask")) {
p = symbol_value("cpu_possible_mask");
mask_addr = (ulong)read_member_64(p, "cpu_possible_mask");
readmem(mask_addr, KVADDR, mask,
BITS_TO_LONGS(kt->cpus) * sizeof(ulong),
"cpu_possible_mask", FAULT_ON_ERROR);
} else {
readmem(symbol_value("cpu_possible_map"), KVADDR, mask,
BITS_TO_LONGS(kt->cpus) * sizeof(ulong),
"cpu_possible_mask", FAULT_ON_ERROR);
}
do {
cpu++;
if (NUM_IN_BITMAP(mask, cpu))
return cpu;
} while(cpu < kt->cpus);
return kt->cpus;
}
static inline uint64_t
jiffies_64_to_clock_t(uint64_t x)
{
/*
* This is only the ideal case. It's more
* complacated in reality.
*/
#if HZ < USER_HZ
x = x * USER_HZ / HZ;
#elif HZ > USER_HZ
x = x / (HZ / USER_HZ);
#endif
return x;
}
/*
* The return value 0 indicates little endian,
* and value 1 indicates big endian.
*/
static inline int
check_endian()
{
int i = 1;
char *p = (char *)&i;
if (*p == 1)
return 0;
else
return 1;
}
static inline uint64_t
ktime_to_ns(ulong ktime_addr)
{
uint64_t ret;
uint32_t sec, nsec;
if (MEMBER_EXISTS("ktime_t", "tv64")) {
/* if member tv64 exists in ktime_t */
readmem(ktime_addr, KVADDR, &ret, sizeof(ulong),
"ktime", FAULT_ON_ERROR);
} else {
if (check_endian()) {
readmem(ktime_addr, KVADDR, &sec, sizeof(ulong),
"ktime sec", FAULT_ON_ERROR);
readmem(ktime_addr + sizeof(uint32_t), KVADDR, &nsec,
sizeof(ulong), "ktime nsec", FAULT_ON_ERROR);
} else {
readmem(ktime_addr, KVADDR, &nsec, sizeof(ulong),
"ktime nsec", FAULT_ON_ERROR);
readmem(ktime_addr + sizeof(uint32_t), KVADDR, &sec,
sizeof(ulong), "ktime sec", FAULT_ON_ERROR);
}
ret = NSEC_PER_SEC * sec + nsec;
}
return ret;
}
static inline int
bitstr_edit(char *buf, int rbot, int rtop, int len)
{
if (len == 0) {
if (rtop == rbot)
sprintf(buf, "%s%d", buf, rtop);
else if (rtop > rbot + 1)
sprintf(buf, "%s%d-%d", buf, rbot, rtop);
else
sprintf(buf, "%s%d,%d", buf, rbot, rtop);
} else {
if (rtop == rbot)
sprintf(buf, "%s,%d", buf, rtop);
else if (rtop > rbot + 1)
sprintf(buf, "%s,%d-%d", buf, rbot, rtop);
else
sprintf(buf, "%s,%d,%d", buf, rbot, rtop);
}
return strlen(buf);
}
static inline void
set_majmin(char *str, unsigned m)
{
if (m == ~0)
strcpy(str, "*");
else
sprintf(str, "%u", m);
}
static inline void
set_access(char *acc, short access)
{
int idx = 0;
memset(acc, 0, ACCLEN);
if (access & ACC_READ)
acc[idx++] = 'r';
if (access & ACC_WRITE)
acc[idx++] = 'w';
if (access & ACC_MKNOD)
acc[idx++] = 'm';
}
static inline char
type_to_char(short type)
{
if (type == DEV_ALL)
return 'a';
if (type == DEV_CHAR)
return 'c';
if (type == DEV_BLOCK)
return 'b';
return 'X';
}
static char *
hugepage_fmt(char *buf, uint64_t size)
{
if (size >= (1UL << 30))
sprintf(buf, "%luGB", size >> 30);
else if (size >= (1UL << 20))
sprintf(buf, "%luMB", size >> 20);
else
sprintf(buf, "%luKB", size >> 10);
return buf;
}
static inline int
is_root_mem_cgroup(ulong mem_addr)
{
ulong root_mem_addr;
if (symbol_exists("root_mem_cgroup"))
readmem(symbol_value("root_mem_cgroup"), KVADDR, &root_mem_addr,
sizeof(ulong), "root_mem_cgroup", FAULT_ON_ERROR);
else
return 1;
if (mem_addr == root_mem_addr)
return 1;
return 0;
}
static inline char *
get_dentry_path(ulong dentry_addr, char *path, int len)
{
char buf[FILENAME_MAX] = {0};
int qstr_len;
ulong name_addr;
memset(path, 0, len);
if (!readmem(dentry_addr + offset_table.dentry_d_name + offset_table.qstr_len,
KVADDR, &qstr_len, sizeof(unsigned int),
"qstr_len", FAULT_ON_ERROR))
return NULL;
if (!readmem(dentry_addr + offset_table.dentry_d_name + offset_table.qstr_name,
KVADDR, &name_addr, sizeof(char *), "name_addr", FAULT_ON_ERROR))
return NULL;
if (!readmem(name_addr, KVADDR, buf, qstr_len, "qstr_name", FAULT_ON_ERROR))
return NULL;
strncpy(path, buf, qstr_len);
return path;
}
static int
read_whitelist(struct cgroup_spec *group_list, ulong whitelist_addr, int behavior)
{
uint32_t major, minor;
short type, access;
char maj[MAJMINLEN], min[MAJMINLEN], acc[ACCLEN];
if (!whitelist_addr)
return -1;
if (behavior == 0) {
set_majmin(maj, ~0);
set_majmin(min, ~0);
set_access(acc, ACC_MASK);
fprintf(fp, "%s.list: %c %s:%s %s\n", group_list->subsys_str,
type_to_char(DEV_ALL), maj, min, acc);
} else {
if (!readmem(whitelist_addr + devices_offset_table.item_major,
KVADDR, &major, sizeof(uint32_t), "item_major",
FAULT_ON_ERROR))
return -1;
set_majmin(maj, major);
if (!readmem(whitelist_addr + devices_offset_table.item_minor,
KVADDR, &minor, sizeof(uint32_t), "item_minor",
FAULT_ON_ERROR))
return -1;
set_majmin(min, minor);
if (!readmem(whitelist_addr + devices_offset_table.item_access,
KVADDR, &access, sizeof(short), "item_access",
FAULT_ON_ERROR))
return -1;
set_access(acc, access);
if (!readmem(whitelist_addr + devices_offset_table.item_type,
KVADDR, &type, sizeof(short), "item_type",
FAULT_ON_ERROR))
return -1;
fprintf(fp, "%s.list: %c %s:%s %s\n", group_list->subsys_str,
type_to_char(type), maj, min, acc);
}
return 0;
}
/*
* Read the value of member. Only size_t of member matters.
* Type should be maintained by the caller.
*/
static int64_t
read_member_64(ulong ptr, char *str)
{
int64_t val = 0;
readmem(ptr, KVADDR, &val, sizeof(int64_t),
str, FAULT_ON_ERROR);
return val;
}
/*
* Read the value of member. Only size_t of member matters.
* Type should be maintained by the caller.
*/
static int32_t
read_member_32(ulong ptr, char *str)
{
int32_t val = 0;
readmem(ptr, KVADDR, &val, sizeof(int32_t),
str, FAULT_ON_ERROR);
return val;
}
/*
* Read the value of member. Only size_t of member matters.
* Type should be maintained by the caller.
*/
static long
read_member_long(ulong ptr)
{
long val = 0;
readmem(ptr, KVADDR, &val, sizeof(long),
"member value", FAULT_ON_ERROR);
return val;
}
static uint64_t
read_res_counter(ulong counter_addr, long off, char *param)
{
uint64_t val = 0;
if (counter_addr == -1 || off == -1)
return 0;
val = (uint64_t)read_member_64(counter_addr + off, "res_counter");
if (param)
fprintf(fp, "%s: %lu\n", param, val);