source: mainline/uspace/app/ps/ps.c@ 516adce

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 516adce was 1ba37fa, checked in by Stanislav Kozina <stanislav.kozina@…>, 16 years ago

Removed useless cycles sum, using ucycles + kcycles instead.

  • Property mode set to 100644
File size: 5.2 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;
85 char usuffix, ksuffix;
86 order(taskinfo.ucycles, &ucycles, &usuffix);
87 order(taskinfo.kcycles, &kcycles, &ksuffix);
88 printf("%8llu %8u %8u %12llu%c %12llu%c %s\n", tasks[i],
89 taskinfo.thread_count, taskinfo.pages, ucycles, usuffix,
90 kcycles, ksuffix, taskinfo.name);
91 }
92
93 free(tasks);
94}
95
96static void list_threads(task_id_t taskid)
97{
98 int thread_count = THREAD_COUNT;
99 thread_info_t *threads = malloc(thread_count * sizeof(thread_info_t));
100 int result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
101
102 while (result > thread_count) {
103 thread_count *= 2;
104 threads = realloc(threads, thread_count * sizeof(thread_info_t));
105 result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
106 }
107
108 if (result == 0) {
109 printf("No task with given pid!\n");
110 exit(1);
111 }
112
113 int i;
114 printf(" ID State CPU Prio [k]uCycles [k]kcycles Cycle fault\n");
115 for (i = 0; i < result; ++i) {
116 uint64_t ucycles, kcycles;
117 char usuffix, ksuffix;
118 order(threads[i].ucycles, &ucycles, &usuffix);
119 order(threads[i].kcycles, &kcycles, &ksuffix);
120 printf("%6llu %-8s %4u %6d %12llu%c %12llu%c\n", threads[i].tid,
121 thread_states[threads[i].state], threads[i].cpu,
122 threads[i].priority, ucycles, usuffix,
123 kcycles, ksuffix);
124 }
125
126 free(threads);
127}
128
129static void echo_load(void)
130{
131 unsigned long load[3];
132 get_load(load);
133 printf("load avarage: ");
134 print_load_fragment(load[0], 2);
135 puts(" ");
136 print_load_fragment(load[1], 2);
137 puts(" ");
138 print_load_fragment(load[2], 2);
139 puts("\n");
140}
141
142static void echo_cpus(void)
143{
144 size_t cpu_count = sysinfo_value("cpu.count");
145 printf("Found %u cpu's:\n", cpu_count);
146 uspace_cpu_info_t *cpus = malloc(cpu_count * sizeof(uspace_cpu_info_t));
147 get_cpu_info(cpus);
148 size_t i;
149 for (i = 0; i < cpu_count; ++i) {
150 printf("%2u (%4u Mhz): Busy ticks: %6llu, Idle ticks: %6llu\n", cpus[i].id,
151 (size_t)cpus[i].frequency_mhz, cpus[i].busy_ticks, cpus[i].idle_ticks);
152 }
153}
154
155static void usage()
156{
157 printf("Usage: ps [-t pid|-l|-c]\n");
158}
159
160int main(int argc, char *argv[])
161{
162 --argc; ++argv;
163
164 if (argc > 0)
165 {
166 if (str_cmp(*argv, "-t") == 0) {
167 --argc; ++argv;
168 if (argc != 1) {
169 printf("Bad argument count!\n");
170 usage();
171 exit(1);
172 }
173 task_id_t taskid = strtol(*argv, NULL, 10);
174 list_threads(taskid);
175 } else if (str_cmp(*argv, "-l") == 0) {
176 --argc; ++argv;
177 if (argc != 0) {
178 printf("Bad argument count!\n");
179 usage();
180 exit(1);
181 }
182 echo_load();
183 } else if (str_cmp(*argv, "-c") == 0) {
184 --argc; ++argv;
185 if (argc != 0) {
186 printf("Bad argument count!\n");
187 usage();
188 exit(1);
189 }
190 echo_cpus();
191 } else {
192 printf("Unknown argument %s!\n", *argv);
193 usage();
194 exit(1);
195 }
196 } else {
197 list_tasks();
198 }
199
200 return 0;
201}
202
203/** @}
204 */
Note: See TracBrowser for help on using the repository browser.