source: mainline/uspace/app/top/top.c@ dd6c71c

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

top echoes counts of tasks

  • Property mode set to 100644
File size: 3.1 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 top
30 * @brief Top utility.
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <io/console.h>
41#include <uptime.h>
42#include <task.h>
43#include <thread.h>
44#include <sys/time.h>
45#include <load.h>
46#include "screen.h"
47#include "input.h"
48#include "top.h"
49#include "ps.h"
50
51#define UPDATE_INTERVAL 1
52
53#define DAY 86400
54#define HOUR 3600
55#define MINUTE 60
56
57static void read_vars(data_t *target)
58{
59 /* Read current time */
60 struct timeval time;
61 if (gettimeofday(&time, NULL) != 0) {
62 printf("Cannot get time of day!\n");
63 exit(1);
64 }
65 target->hours = (time.tv_sec % DAY) / HOUR;
66 target->minutes = (time.tv_sec % HOUR) / MINUTE;
67 target->seconds = time.tv_sec % MINUTE;
68
69 /* Read uptime */
70 uint64_t uptime;
71 get_uptime(&uptime);
72 target->uptime_d = uptime / DAY;
73 target->uptime_h = (uptime % DAY) / HOUR;
74 target->uptime_m = (uptime % HOUR) / MINUTE;
75 target->uptime_s = uptime % MINUTE;
76
77 /* Read load */
78 get_load(target->load);
79
80 /* Read task ids */
81 target->task_count = get_tasks(&target->tasks);
82}
83
84int main(int argc, char *argv[])
85{
86 data_t old_data;
87 data_t new_data;
88
89 /* Read initial stats */
90 printf("Reading initial data...\n");
91 read_vars(&old_data);
92 sleep(UPDATE_INTERVAL);
93 read_vars(&new_data);
94
95 screen_init();
96
97 /* And paint screen until death... */
98 while (true) {
99 char c = tgetchar(UPDATE_INTERVAL);
100 if (c < 0) {
101 read_vars(&new_data);
102 print_data(&new_data);
103 continue;
104 }
105 switch (c) {
106 case 'q':
107 return 0;
108 default:
109 moveto(10,10);
110 printf("Unknown command: %c", c);
111 fflush(stdout);
112 break;
113 }
114
115 }
116
117 free(new_data.tasks);
118 puts("\n\n");
119 fflush(stdout);
120 return 0;
121}
122
123/** @}
124 */
Note: See TracBrowser for help on using the repository browser.