source: mainline/uspace/app/ps/ps.c@ 175ad5c6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 175ad5c6 was 62550dce, checked in by Stanislav Kozina <stanislav.kozina@…>, 15 years ago

ps -c echoes info about CPU's
cpu count is accessible through sysinfo("cpu.count")

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2010 Stanislav Kozina
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup ps
30 * @brief Task lister.
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdio.h>
38#include <task.h>
39#include <thread.h>
40#include <ps.h>
41#include <errno.h>
42#include <stdlib.h>
43#include <malloc.h>
44#include <load.h>
45#include <sysinfo.h>
46
47#include "func.h"
48
49#define TASK_COUNT 10
50#define THREAD_COUNT 50
51
52#define ECHOLOAD1(x) ((x) >> 11)
53#define ECHOLOAD2(x) (((x) & 0x7ff) / 2)
54
55/** Thread states */
56static const char *thread_states[] = {
57 "Invalid",
58 "Running",
59 "Sleeping",
60 "Ready",
61 "Entering",
62 "Exiting",
63 "Lingering"
64};
65
66static void list_tasks(void)
67{
68 int task_count = TASK_COUNT;
69 task_id_t *tasks = malloc(task_count * sizeof(task_id_t));
70 int result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
71
72 while (result > task_count) {
73 task_count *= 2;
74 tasks = realloc(tasks, task_count * sizeof(task_id_t));
75 result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
76 }
77
78 printf(" ID Threads Pages uCycles kCycles Cycle fault Name\n");
79
80 int i;
81 for (i = 0; i < result; ++i) {
82 task_info_t taskinfo;
83 get_task_info(tasks[i], &taskinfo);
84 uint64_t ucycles, kcycles, fault;
85 char usuffix, ksuffix, fsuffix;
86 order(taskinfo.ucycles, &ucycles, &usuffix);
87 order(taskinfo.kcycles, &kcycles, &ksuffix);
88 order((taskinfo.kcycles + taskinfo.ucycles) - taskinfo.cycles, &fault, &fsuffix);
89 printf("%8llu %8u %8u %12llu%c %12llu%c %12llu%c %s\n", tasks[i],
90 taskinfo.thread_count, taskinfo.pages, ucycles, usuffix,
91 kcycles, ksuffix, fault, fsuffix, taskinfo.name);
92 }
93
94 free(tasks);
95}
96
97static void list_threads(task_id_t taskid)
98{
99 int thread_count = THREAD_COUNT;
100 thread_info_t *threads = malloc(thread_count * sizeof(thread_info_t));
101 int result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
102
103 while (result > thread_count) {
104 thread_count *= 2;
105 threads = realloc(threads, thread_count * sizeof(thread_info_t));
106 result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
107 }
108
109 if (result == 0) {
110 printf("No task with given pid!\n");
111 exit(1);
112 }
113
114 int i;
115 printf(" ID State CPU Prio [k]uCycles [k]kcycles Cycle fault\n");
116 for (i = 0; i < result; ++i) {
117 uint64_t ucycles, kcycles, fault;
118 char usuffix, ksuffix, fsuffix;
119 order(threads[i].ucycles, &ucycles, &usuffix);
120 order(threads[i].kcycles, &kcycles, &ksuffix);
121 order((threads[i].kcycles + threads[i].ucycles) - threads[i].cycles, &fault, &fsuffix);
122 printf("%6llu %-8s %4u %6d %12llu%c %12llu%c %12llu%c\n", threads[i].tid,
123 thread_states[threads[i].state], threads[i].cpu,
124 threads[i].priority, ucycles, usuffix,
125 kcycles, ksuffix, fault, fsuffix);
126 }
127
128 free(threads);
129}
130
131static void echo_load(void)
132{
133 unsigned long load[3];
134 get_load(load);
135 printf("load avarage: ");
136 print_load_fragment(load[0], 2);
137 puts(" ");
138 print_load_fragment(load[1], 2);
139 puts(" ");
140 print_load_fragment(load[2], 2);
141 puts("\n");
142}
143
144static void echo_cpus(void)
145{
146 size_t cpu_count = sysinfo_value("cpu.count");
147 printf("Found %u cpu's:\n", cpu_count);
148 uspace_cpu_info_t *cpus = malloc(cpu_count * sizeof(uspace_cpu_info_t));
149 get_cpu_info(cpus);
150 size_t i;
151 for (i = 0; i < cpu_count; ++i) {
152 printf("%2u (%4u Mhz): Busy ticks: %8llu, Idle ticks: %8llu\n", cpus[i].id,
153 (size_t)cpus[i].frequency_mhz, cpus[i].busy_ticks, cpus[i].idle_ticks);
154 }
155}
156
157static void usage()
158{
159 printf("Usage: ps [-t pid|-l|-c]\n");
160}
161
162int main(int argc, char *argv[])
163{
164 --argc; ++argv;
165
166 if (argc > 0)
167 {
168 if (str_cmp(*argv, "-t") == 0) {
169 --argc; ++argv;
170 if (argc != 1) {
171 printf("Bad argument count!\n");
172 usage();
173 exit(1);
174 }
175 task_id_t taskid = strtol(*argv, NULL, 10);
176 list_threads(taskid);
177 } else if (str_cmp(*argv, "-l") == 0) {
178 --argc; ++argv;
179 if (argc != 0) {
180 printf("Bad argument count!\n");
181 usage();
182 exit(1);
183 }
184 echo_load();
185 } else if (str_cmp(*argv, "-c") == 0) {
186 --argc; ++argv;
187 if (argc != 0) {
188 printf("Bad argument count!\n");
189 usage();
190 exit(1);
191 }
192 echo_cpus();
193 } else {
194 printf("Unknown argument %s!\n", *argv);
195 usage();
196 exit(1);
197 }
198 } else {
199 list_tasks();
200 }
201
202 return 0;
203}
204
205/** @}
206 */
Note: See TracBrowser for help on using the repository browser.