source: mainline/uspace/app/ps/ps.c@ a2a00e8

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

Tweaking ps
ps -t echoes also current CPU

  • Property mode set to 100644
File size: 3.7 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 <string.h> */
44#include <malloc.h>
45
46#define TASK_COUNT 10
47#define THREAD_COUNT 50
48
49/** Thread states */
50static const char *thread_states[] = {
51 "Invalid",
52 "Running",
53 "Sleeping",
54 "Ready",
55 "Entering",
56 "Exiting",
57 "Lingering"
58};
59
60static void list_tasks(void)
61{
62 int task_count = TASK_COUNT;
63 task_id_t *tasks = malloc(task_count * sizeof(task_id_t));
64 int result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
65
66 while (result > task_count) {
67 task_count *= 2;
68 tasks = realloc(tasks, task_count * sizeof(task_id_t));
69 result = get_task_ids(tasks, sizeof(task_id_t) * task_count);
70 }
71
72 printf(" ID Threads Pages [k]Cycles Name\n");
73
74 int i;
75 for (i = 0; i < result; ++i) {
76 task_info_t taskinfo;
77 get_task_info(tasks[i], &taskinfo);
78 printf("%8llu %8u %8u %12llu %s\n", tasks[i], taskinfo.thread_count,
79 taskinfo.pages, taskinfo.cycles / 1000, taskinfo.name);
80 }
81}
82
83static void list_threads(task_id_t taskid)
84{
85 int thread_count = THREAD_COUNT;
86 thread_info_t *threads = malloc(thread_count * sizeof(thread_info_t));
87 int result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
88
89 while (result > thread_count) {
90 thread_count *= 2;
91 threads = realloc(threads, thread_count * sizeof(thread_info_t));
92 result = get_task_threads(taskid, threads, sizeof(thread_info_t) * thread_count);
93 }
94
95 if (result == 0) {
96 printf("No task with given pid!\n");
97 exit(1);
98 }
99
100 int i;
101 printf(" ID State CPU Prio [k]Cycles\n");
102 for (i = 0; i < result; ++i) {
103 printf("%6llu %-8s %4u %6d %12llu\n", threads[i].tid, thread_states[threads[i].state],
104 threads[i].cpu, threads[i].priority, threads[i].cycles / 1000);
105 }
106}
107
108static void usage()
109{
110 printf("Usage: ps [-t pid]\n");
111}
112
113int main(int argc, char *argv[])
114{
115 --argc; ++argv;
116
117 if (argc > 0)
118 {
119 if (str_cmp(*argv, "-t") == 0) {
120 --argc; ++argv;
121 if (argc != 1) {
122 printf("Bad argument count!\n");
123 usage();
124 exit(1);
125 }
126 task_id_t taskid = strtol(*argv, NULL, 10);
127 list_threads(taskid);
128 } else {
129 printf("Unknown argument %s!\n", *argv);
130 usage();
131 exit(1);
132 }
133
134 } else {
135 list_tasks();
136 }
137
138 return 0;
139}
140
141/** @}
142 */
Note: See TracBrowser for help on using the repository browser.