2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "alloc-util.h"
32 #include "bus-error.h"
34 #include "cgroup-util.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "process-util.h"
41 #include "stdio-util.h"
42 #include "terminal-util.h"
43 #include "unit-name.h"
46 typedef struct Group
{
56 unsigned cpu_iteration
;
63 unsigned io_iteration
;
64 uint64_t io_input
, io_output
;
66 uint64_t io_input_bps
, io_output_bps
;
69 static unsigned arg_depth
= 3;
70 static unsigned arg_iterations
= (unsigned) -1;
71 static bool arg_batch
= false;
72 static bool arg_raw
= false;
73 static usec_t arg_delay
= 1*USEC_PER_SEC
;
74 static char* arg_machine
= NULL
;
75 static bool arg_recursive
= true;
79 COUNT_USERSPACE_PROCESSES
,
81 } arg_count
= COUNT_PIDS
;
89 } arg_order
= ORDER_CPU
;
94 } arg_cpu_type
= CPU_PERCENT
;
96 static void group_free(Group
*g
) {
103 static void group_hashmap_clear(Hashmap
*h
) {
106 while ((g
= hashmap_steal_first(h
)))
110 static void group_hashmap_free(Hashmap
*h
) {
111 group_hashmap_clear(h
);
115 static const char *maybe_format_bytes(char *buf
, size_t l
, bool is_valid
, uint64_t t
) {
119 snprintf(buf
, l
, "%jd", t
);
122 return format_bytes(buf
, l
, t
);
126 const char *controller
,
140 g
= hashmap_get(a
, path
);
142 g
= hashmap_get(b
, path
);
148 g
->path
= strdup(path
);
154 r
= hashmap_put(a
, g
->path
, g
);
160 r
= hashmap_move_one(a
, b
, path
);
164 g
->cpu_valid
= g
->memory_valid
= g
->io_valid
= g
->n_tasks_valid
= false;
168 if (streq(controller
, SYSTEMD_CGROUP_CONTROLLER
) && IN_SET(arg_count
, COUNT_ALL_PROCESSES
, COUNT_USERSPACE_PROCESSES
)) {
169 _cleanup_fclose_
FILE *f
= NULL
;
172 r
= cg_enumerate_processes(controller
, path
, &f
);
179 while (cg_read_pid(f
, &pid
) > 0) {
181 if (arg_count
== COUNT_USERSPACE_PROCESSES
&& is_kernel_thread(pid
) > 0)
188 g
->n_tasks_valid
= true;
190 } else if (streq(controller
, "pids") && arg_count
== COUNT_PIDS
) {
191 _cleanup_free_
char *p
= NULL
, *v
= NULL
;
193 r
= cg_get_path(controller
, path
, "pids.current", &p
);
197 r
= read_one_line_file(p
, &v
);
203 r
= safe_atou64(v
, &g
->n_tasks
);
208 g
->n_tasks_valid
= true;
210 } else if (streq(controller
, "cpuacct") && cg_unified() <= 0) {
211 _cleanup_free_
char *p
= NULL
, *v
= NULL
;
215 r
= cg_get_path(controller
, path
, "cpuacct.usage", &p
);
219 r
= read_one_line_file(p
, &v
);
225 r
= safe_atou64(v
, &new_usage
);
229 timestamp
= now_nsec(CLOCK_MONOTONIC
);
231 if (g
->cpu_iteration
== iteration
- 1 &&
232 (nsec_t
) new_usage
> g
->cpu_usage
) {
236 x
= timestamp
- g
->cpu_timestamp
;
240 y
= (nsec_t
) new_usage
- g
->cpu_usage
;
241 g
->cpu_fraction
= (double) y
/ (double) x
;
245 g
->cpu_usage
= (nsec_t
) new_usage
;
246 g
->cpu_timestamp
= timestamp
;
247 g
->cpu_iteration
= iteration
;
249 } else if (streq(controller
, "memory")) {
250 _cleanup_free_
char *p
= NULL
, *v
= NULL
;
252 if (cg_unified() <= 0)
253 r
= cg_get_path(controller
, path
, "memory.usage_in_bytes", &p
);
255 r
= cg_get_path(controller
, path
, "memory.current", &p
);
259 r
= read_one_line_file(p
, &v
);
265 r
= safe_atou64(v
, &g
->memory
);
270 g
->memory_valid
= true;
272 } else if (streq(controller
, "blkio") && cg_unified() <= 0) {
273 _cleanup_fclose_
FILE *f
= NULL
;
274 _cleanup_free_
char *p
= NULL
;
275 uint64_t wr
= 0, rd
= 0;
278 r
= cg_get_path(controller
, path
, "blkio.io_service_bytes", &p
);
290 char line
[LINE_MAX
], *l
;
293 if (!fgets(line
, sizeof(line
), f
))
297 l
+= strcspn(l
, WHITESPACE
);
298 l
+= strspn(l
, WHITESPACE
);
300 if (first_word(l
, "Read")) {
303 } else if (first_word(l
, "Write")) {
309 l
+= strspn(l
, WHITESPACE
);
310 r
= safe_atou64(l
, &k
);
317 timestamp
= now_nsec(CLOCK_MONOTONIC
);
319 if (g
->io_iteration
== iteration
- 1) {
322 x
= (uint64_t) (timestamp
- g
->io_timestamp
);
326 if (rd
> g
->io_input
)
327 yr
= rd
- g
->io_input
;
331 if (wr
> g
->io_output
)
332 yw
= wr
- g
->io_output
;
336 if (yr
> 0 || yw
> 0) {
337 g
->io_input_bps
= (yr
* 1000000000ULL) / x
;
338 g
->io_output_bps
= (yw
* 1000000000ULL) / x
;
345 g
->io_timestamp
= timestamp
;
346 g
->io_iteration
= iteration
;
355 static int refresh_one(
356 const char *controller
,
364 _cleanup_closedir_
DIR *d
= NULL
;
372 if (depth
> arg_depth
)
375 r
= process(controller
, path
, a
, b
, iteration
, &ours
);
379 r
= cg_enumerate_subgroups(controller
, path
, &d
);
386 _cleanup_free_
char *fn
= NULL
, *p
= NULL
;
389 r
= cg_read_subgroup(d
, &fn
);
395 p
= strjoin(path
, "/", fn
, NULL
);
399 path_kill_slashes(p
);
401 r
= refresh_one(controller
, p
, a
, b
, iteration
, depth
+ 1, &child
);
406 IN_SET(arg_count
, COUNT_ALL_PROCESSES
, COUNT_USERSPACE_PROCESSES
) &&
408 child
->n_tasks_valid
&&
409 streq(controller
, SYSTEMD_CGROUP_CONTROLLER
)) {
411 /* Recursively sum up processes */
413 if (ours
->n_tasks_valid
)
414 ours
->n_tasks
+= child
->n_tasks
;
416 ours
->n_tasks
= child
->n_tasks
;
417 ours
->n_tasks_valid
= true;
428 static int refresh(const char *root
, Hashmap
*a
, Hashmap
*b
, unsigned iteration
) {
433 r
= refresh_one(SYSTEMD_CGROUP_CONTROLLER
, root
, a
, b
, iteration
, 0, NULL
);
436 r
= refresh_one("cpuacct", root
, a
, b
, iteration
, 0, NULL
);
439 r
= refresh_one("memory", root
, a
, b
, iteration
, 0, NULL
);
442 r
= refresh_one("blkio", root
, a
, b
, iteration
, 0, NULL
);
445 r
= refresh_one("pids", root
, a
, b
, iteration
, 0, NULL
);
452 static int group_compare(const void*a
, const void *b
) {
453 const Group
*x
= *(Group
**)a
, *y
= *(Group
**)b
;
455 if (arg_order
!= ORDER_TASKS
|| arg_recursive
) {
456 /* Let's make sure that the parent is always before
457 * the child. Except when ordering by tasks and
458 * recursive summing is off, since that is actually
459 * not accumulative for all children. */
461 if (path_startswith(y
->path
, x
->path
))
463 if (path_startswith(x
->path
, y
->path
))
473 if (arg_cpu_type
== CPU_PERCENT
) {
474 if (x
->cpu_valid
&& y
->cpu_valid
) {
475 if (x
->cpu_fraction
> y
->cpu_fraction
)
477 else if (x
->cpu_fraction
< y
->cpu_fraction
)
479 } else if (x
->cpu_valid
)
481 else if (y
->cpu_valid
)
484 if (x
->cpu_usage
> y
->cpu_usage
)
486 else if (x
->cpu_usage
< y
->cpu_usage
)
493 if (x
->n_tasks_valid
&& y
->n_tasks_valid
) {
494 if (x
->n_tasks
> y
->n_tasks
)
496 else if (x
->n_tasks
< y
->n_tasks
)
498 } else if (x
->n_tasks_valid
)
500 else if (y
->n_tasks_valid
)
506 if (x
->memory_valid
&& y
->memory_valid
) {
507 if (x
->memory
> y
->memory
)
509 else if (x
->memory
< y
->memory
)
511 } else if (x
->memory_valid
)
513 else if (y
->memory_valid
)
519 if (x
->io_valid
&& y
->io_valid
) {
520 if (x
->io_input_bps
+ x
->io_output_bps
> y
->io_input_bps
+ y
->io_output_bps
)
522 else if (x
->io_input_bps
+ x
->io_output_bps
< y
->io_input_bps
+ y
->io_output_bps
)
524 } else if (x
->io_valid
)
526 else if (y
->io_valid
)
530 return path_compare(x
->path
, y
->path
);
533 static void display(Hashmap
*a
) {
538 unsigned rows
, n
= 0, j
, maxtcpu
= 0, maxtpath
= 3; /* 3 for ellipsize() to work properly */
539 char buffer
[MAX3(21, FORMAT_BYTES_MAX
, FORMAT_TIMESPAN_MAX
)];
544 fputs(ANSI_HOME_CLEAR
, stdout
);
546 array
= alloca(sizeof(Group
*) * hashmap_size(a
));
548 HASHMAP_FOREACH(g
, a
, i
)
549 if (g
->n_tasks_valid
|| g
->cpu_valid
|| g
->memory_valid
|| g
->io_valid
)
552 qsort_safe(array
, n
, sizeof(Group
*), group_compare
);
554 /* Find the longest names in one run */
555 for (j
= 0; j
< n
; j
++) {
556 unsigned cputlen
, pathtlen
;
558 format_timespan(buffer
, sizeof(buffer
), (usec_t
) (array
[j
]->cpu_usage
/ NSEC_PER_USEC
), 0);
559 cputlen
= strlen(buffer
);
560 maxtcpu
= MAX(maxtcpu
, cputlen
);
562 pathtlen
= strlen(array
[j
]->path
);
563 maxtpath
= MAX(maxtpath
, pathtlen
);
566 if (arg_cpu_type
== CPU_PERCENT
)
567 xsprintf(buffer
, "%6s", "%CPU");
569 xsprintf(buffer
, "%*s", maxtcpu
, "CPU Time");
576 const char *on
, *off
;
578 path_columns
= columns() - 36 - strlen(buffer
);
579 if (path_columns
< 10)
582 on
= ansi_highlight_underline();
583 off
= ansi_underline();
585 printf("%s%s%-*s%s %s%7s%s %s%s%s %s%8s%s %s%8s%s %s%8s%s%s\n",
587 arg_order
== ORDER_PATH
? on
: "", path_columns
, "Control Group",
588 arg_order
== ORDER_PATH
? off
: "",
589 arg_order
== ORDER_TASKS
? on
: "", arg_count
== COUNT_PIDS
? "Tasks" : arg_count
== COUNT_USERSPACE_PROCESSES
? "Procs" : "Proc+",
590 arg_order
== ORDER_TASKS
? off
: "",
591 arg_order
== ORDER_CPU
? on
: "", buffer
,
592 arg_order
== ORDER_CPU
? off
: "",
593 arg_order
== ORDER_MEMORY
? on
: "", "Memory",
594 arg_order
== ORDER_MEMORY
? off
: "",
595 arg_order
== ORDER_IO
? on
: "", "Input/s",
596 arg_order
== ORDER_IO
? off
: "",
597 arg_order
== ORDER_IO
? on
: "", "Output/s",
598 arg_order
== ORDER_IO
? off
: "",
601 path_columns
= maxtpath
;
603 for (j
= 0; j
< n
; j
++) {
604 _cleanup_free_
char *ellipsized
= NULL
;
607 if (on_tty() && j
+ 6 > rows
)
612 path
= isempty(g
->path
) ? "/" : g
->path
;
613 ellipsized
= ellipsize(path
, path_columns
, 33);
614 printf("%-*s", path_columns
, ellipsized
?: path
);
616 if (g
->n_tasks_valid
)
617 printf(" %7" PRIu64
, g
->n_tasks
);
621 if (arg_cpu_type
== CPU_PERCENT
) {
623 printf(" %6.1f", g
->cpu_fraction
*100);
627 printf(" %*s", maxtcpu
, format_timespan(buffer
, sizeof(buffer
), (usec_t
) (g
->cpu_usage
/ NSEC_PER_USEC
), 0));
629 printf(" %8s", maybe_format_bytes(buffer
, sizeof(buffer
), g
->memory_valid
, g
->memory
));
630 printf(" %8s", maybe_format_bytes(buffer
, sizeof(buffer
), g
->io_valid
, g
->io_input_bps
));
631 printf(" %8s", maybe_format_bytes(buffer
, sizeof(buffer
), g
->io_valid
, g
->io_output_bps
));
637 static void help(void) {
638 printf("%s [OPTIONS...]\n\n"
639 "Show top control groups by their resource usage.\n\n"
640 " -h --help Show this help\n"
641 " --version Show package version\n"
642 " -p --order=path Order by path\n"
643 " -t --order=tasks Order by number of tasks/processes\n"
644 " -c --order=cpu Order by CPU load (default)\n"
645 " -m --order=memory Order by memory load\n"
646 " -i --order=io Order by IO load\n"
647 " -r --raw Provide raw (not human-readable) numbers\n"
648 " --cpu=percentage Show CPU usage as percentage (default)\n"
649 " --cpu=time Show CPU usage as time\n"
650 " -P Count userspace processes instead of tasks (excl. kernel)\n"
651 " -k Count all processes instead of tasks (incl. kernel)\n"
652 " --recursive=BOOL Sum up process count recursively\n"
653 " -d --delay=DELAY Delay between updates\n"
654 " -n --iterations=N Run for N iterations before exiting\n"
655 " -b --batch Run in batch mode, accepting no input\n"
656 " --depth=DEPTH Maximum traversal depth (default: %u)\n"
657 " -M --machine= Show container\n"
658 , program_invocation_short_name
, arg_depth
);
661 static int parse_argv(int argc
, char *argv
[]) {
671 static const struct option options
[] = {
672 { "help", no_argument
, NULL
, 'h' },
673 { "version", no_argument
, NULL
, ARG_VERSION
},
674 { "delay", required_argument
, NULL
, 'd' },
675 { "iterations", required_argument
, NULL
, 'n' },
676 { "batch", no_argument
, NULL
, 'b' },
677 { "raw", no_argument
, NULL
, 'r' },
678 { "depth", required_argument
, NULL
, ARG_DEPTH
},
679 { "cpu", optional_argument
, NULL
, ARG_CPU_TYPE
},
680 { "order", required_argument
, NULL
, ARG_ORDER
},
681 { "recursive", required_argument
, NULL
, ARG_RECURSIVE
},
682 { "machine", required_argument
, NULL
, 'M' },
686 bool recursive_unset
= false;
692 while ((c
= getopt_long(argc
, argv
, "hptcmin:brd:kPM:", options
, NULL
)) >= 0)
705 if (streq(optarg
, "time"))
706 arg_cpu_type
= CPU_TIME
;
707 else if (streq(optarg
, "percentage"))
708 arg_cpu_type
= CPU_PERCENT
;
710 log_error("Unknown argument to --cpu=: %s", optarg
);
714 arg_cpu_type
= CPU_TIME
;
719 r
= safe_atou(optarg
, &arg_depth
);
721 log_error("Failed to parse depth parameter.");
728 r
= parse_sec(optarg
, &arg_delay
);
729 if (r
< 0 || arg_delay
<= 0) {
730 log_error("Failed to parse delay parameter.");
737 r
= safe_atou(optarg
, &arg_iterations
);
739 log_error("Failed to parse iterations parameter.");
754 arg_order
= ORDER_PATH
;
758 arg_order
= ORDER_TASKS
;
762 arg_order
= ORDER_CPU
;
766 arg_order
= ORDER_MEMORY
;
770 arg_order
= ORDER_IO
;
774 if (streq(optarg
, "path"))
775 arg_order
= ORDER_PATH
;
776 else if (streq(optarg
, "tasks"))
777 arg_order
= ORDER_TASKS
;
778 else if (streq(optarg
, "cpu"))
779 arg_order
= ORDER_CPU
;
780 else if (streq(optarg
, "memory"))
781 arg_order
= ORDER_MEMORY
;
782 else if (streq(optarg
, "io"))
783 arg_order
= ORDER_IO
;
785 log_error("Invalid argument to --order=: %s", optarg
);
791 arg_count
= COUNT_ALL_PROCESSES
;
795 arg_count
= COUNT_USERSPACE_PROCESSES
;
799 r
= parse_boolean(optarg
);
801 log_error("Failed to parse --recursive= argument: %s", optarg
);
806 recursive_unset
= r
== 0;
810 arg_machine
= optarg
;
817 assert_not_reached("Unhandled option");
821 log_error("Too many arguments.");
825 if (recursive_unset
&& arg_count
== COUNT_PIDS
) {
826 log_error("Non-recursive counting is only supported when counting processes, not tasks. Use -P or -k.");
833 static const char* counting_what(void) {
834 if (arg_count
== COUNT_PIDS
)
836 else if (arg_count
== COUNT_ALL_PROCESSES
)
837 return "all processes (incl. kernel)";
839 return "userspace processes (excl. kernel)";
842 static int get_cgroup_root(char **ret
) {
843 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
844 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
845 _cleanup_free_
char *unit
= NULL
, *path
= NULL
;
850 r
= cg_get_root_path(ret
);
852 return log_error_errno(r
, "Failed to get root control group path: %m");
857 m
= strjoina("/run/systemd/machines/", arg_machine
);
858 r
= parse_env_file(m
, NEWLINE
, "SCOPE", &unit
, NULL
);
860 return log_error_errno(r
, "Failed to load machine data: %m");
862 path
= unit_dbus_path_from_name(unit
);
866 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
, false, &bus
);
868 return log_error_errno(r
, "Failed to create bus connection: %m");
870 r
= sd_bus_get_property_string(
872 "org.freedesktop.systemd1",
874 unit_dbus_interface_from_name(unit
),
879 return log_error_errno(r
, "Failed to query unit control group path: %s", bus_error_message(&error
, r
));
884 int main(int argc
, char *argv
[]) {
886 Hashmap
*a
= NULL
, *b
= NULL
;
887 unsigned iteration
= 0;
888 usec_t last_refresh
= 0;
889 bool quit
= false, immediate_refresh
= false;
890 _cleanup_free_
char *root
= NULL
;
893 log_parse_environment();
896 r
= cg_mask_supported(&mask
);
898 log_error_errno(r
, "Failed to determine supported controllers: %m");
902 arg_count
= (mask
& CGROUP_MASK_PIDS
) ? COUNT_PIDS
: COUNT_USERSPACE_PROCESSES
;
904 r
= parse_argv(argc
, argv
);
908 r
= get_cgroup_root(&root
);
910 log_error_errno(r
, "Failed to get root control group path: %m");
914 a
= hashmap_new(&string_hash_ops
);
915 b
= hashmap_new(&string_hash_ops
);
921 signal(SIGWINCH
, columns_lines_cache_reset
);
923 if (arg_iterations
== (unsigned) -1)
924 arg_iterations
= on_tty() ? 0 : 1;
930 char h
[FORMAT_TIMESPAN_MAX
];
932 t
= now(CLOCK_MONOTONIC
);
934 if (t
>= last_refresh
+ arg_delay
|| immediate_refresh
) {
936 r
= refresh(root
, a
, b
, iteration
++);
938 log_error_errno(r
, "Failed to refresh: %m");
942 group_hashmap_clear(b
);
949 immediate_refresh
= false;
954 if (arg_iterations
&& iteration
>= arg_iterations
)
957 if (!on_tty()) /* non-TTY: Empty newline as delimiter between polls */
962 (void) usleep(last_refresh
+ arg_delay
- t
);
964 r
= read_one_char(stdin
, &key
, last_refresh
+ arg_delay
- t
, NULL
);
968 log_error_errno(r
, "Couldn't read key: %m");
973 if (on_tty()) { /* TTY: Clear any user keystroke */
974 fputs("\r \r", stdout
);
984 immediate_refresh
= true;
992 arg_order
= ORDER_PATH
;
996 arg_order
= ORDER_TASKS
;
1000 arg_order
= ORDER_CPU
;
1004 arg_order
= ORDER_MEMORY
;
1008 arg_order
= ORDER_IO
;
1012 arg_cpu_type
= arg_cpu_type
== CPU_TIME
? CPU_PERCENT
: CPU_TIME
;
1016 arg_count
= arg_count
!= COUNT_ALL_PROCESSES
? COUNT_ALL_PROCESSES
: COUNT_PIDS
;
1017 fprintf(stdout
, "\nCounting: %s.", counting_what());
1023 arg_count
= arg_count
!= COUNT_USERSPACE_PROCESSES
? COUNT_USERSPACE_PROCESSES
: COUNT_PIDS
;
1024 fprintf(stdout
, "\nCounting: %s.", counting_what());
1030 if (arg_count
== COUNT_PIDS
)
1031 fprintf(stdout
, "\n\aCannot toggle recursive counting, not available in task counting mode.");
1033 arg_recursive
= !arg_recursive
;
1034 fprintf(stdout
, "\nRecursive process counting: %s", yes_no(arg_recursive
));
1041 if (arg_delay
< USEC_PER_SEC
)
1042 arg_delay
+= USEC_PER_MSEC
*250;
1044 arg_delay
+= USEC_PER_SEC
;
1046 fprintf(stdout
, "\nIncreased delay to %s.", format_timespan(h
, sizeof(h
), arg_delay
, 0));
1052 if (arg_delay
<= USEC_PER_MSEC
*500)
1053 arg_delay
= USEC_PER_MSEC
*250;
1054 else if (arg_delay
< USEC_PER_MSEC
*1250)
1055 arg_delay
-= USEC_PER_MSEC
*250;
1057 arg_delay
-= USEC_PER_SEC
;
1059 fprintf(stdout
, "\nDecreased delay to %s.", format_timespan(h
, sizeof(h
), arg_delay
, 0));
1067 #define ON ANSI_HIGHLIGHT
1068 #define OFF ANSI_NORMAL
1071 "\t<" ON
"p" OFF
"> By path; <" ON
"t" OFF
"> By tasks/procs; <" ON
"c" OFF
"> By CPU; <" ON
"m" OFF
"> By memory; <" ON
"i" OFF
"> By I/O\n"
1072 "\t<" ON
"+" OFF
"> Inc. delay; <" ON
"-" OFF
"> Dec. delay; <" ON
"%%" OFF
"> Toggle time; <" ON
"SPACE" OFF
"> Refresh\n"
1073 "\t<" ON
"P" OFF
"> Toggle count userspace processes; <" ON
"k" OFF
"> Toggle count all processes\n"
1074 "\t<" ON
"r" OFF
"> Count processes recursively; <" ON
"q" OFF
"> Quit");
1081 fprintf(stdout
, "\nUnknown key '\\x%x'. Ignoring.", key
);
1083 fprintf(stdout
, "\nUnknown key '%c'. Ignoring.", key
);
1093 group_hashmap_free(a
);
1094 group_hashmap_free(b
);
1096 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;