source: liacs/os/assignment2/partBCD.c@ 232

Last change on this file since 232 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 5.3 KB
RevLine 
[2]1/*
2 * Rick van der Zwet
3 * 0433373
4 * OS Assigment 2 - Part BCD
5 * Licence: BSD
6 * $Id: partBCD.c 508 2008-03-02 14:18:40Z rick $
7 */
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <sysexits.h>
12#include <time.h>
13#include <unistd.h>
14
15void usage(const int errno) {
16 printf("Usage:\n");
17 printf("-h : This screen\n");
18 printf("-l <int> : <int> times to show loadavg\n");
19 printf("-s <int> : Sleep <int> seconds between every loadavg line\n");
20 exit(errno);
21}
22
23void partB() {
24 char buffer[100];
25 long int uptime_seconds = 0;
26 int i, days, hours, minutes, seconds;
27 FILE * file;
28
29 /* cpuinfo */
30 file = fopen("/proc/cpuinfo", "r");
31 /* move to right line */
32 for (i = 0; i <= 3; i++)
33 while (fgetc(file) != '\n');
34 /* Fix offset */
35 fgets(buffer, 14, file);
36 /* Read data */
37 fgets(buffer, 50, file);
38 fclose(file);
39 printf("CPU Info: %s", buffer);
40
41 /* Kernel version */
42 file = fopen("/proc/sys/kernel/osrelease", "r");
43 fgets(buffer, 50, file);
44 fclose(file);
45 printf("Kernel version: %s", buffer);
46
47 /* Pretty formatted uptime */
48 file = fopen("/proc/uptime", "r");
49 fscanf(file, "%li", &uptime_seconds);
50 fclose(file);
51 days = uptime_seconds / (24 * 3600);
52 uptime_seconds = uptime_seconds % (24 * 3600);
53 hours = uptime_seconds / 3600;
54 uptime_seconds = uptime_seconds % 3600;
55 minutes = uptime_seconds / 60;
56 seconds = uptime_seconds % 60;
57 printf("Uptime (dd:hh:mm:ss): %02i:%02i:%02i:%02i\n",
58 days, hours, minutes, seconds );
59}
60
61void partC() {
62 char buffer[100];
63 long int cpu_user, cpu_user_nice, cpu_system, cpu_idle;
64 long int disk_read_total, disk_write_total, disk_read, disk_write;
65 int i, errno;
66 time_t boot_time, now, uptime;
67 FILE * file;
68
69 /* User,system, idle mode */
70 file = fopen("/proc/stat", "r");
71 fscanf(file, "%*s %li %li %li %li", &cpu_user, &cpu_user_nice, &cpu_system, &cpu_idle);
72 fclose(file);
73
74 printf("Buffer: %s", buffer);
75 printf("CPU User : %10li sec\n", cpu_user + cpu_user_nice);
76 printf("CPU System : %10li sec\n", cpu_system);
77 printf("CPU Idle : %10li sec\n", cpu_idle);
78
79 /* Disk operations, read/write on all disks (sum)
80 * Compatible with 2.6 kernel only Legenda at Documentation/iostats.txt
81 * /proc/diskstat read: field 4, write field 8
82 * Partitions only has 7 fields and could easy be ignored
83 */
84 disk_read = 0;
85 disk_write = 0;
86 file = fopen("/proc/diskstats", "r");
87 /* %*100c = dirty hack to move till EOL */
88 while ((errno = fscanf(file, "%*s %*s %*s %li %*s %*s %*s %li %*100c",
89 &disk_read, &disk_write)) != EOF) {
90 if (errno == 0)
91 continue;
92 disk_read_total += disk_read;
93 disk_write_total += disk_write;
94 }
95 fclose(file);
96
97 printf("Disk read operations : %li\n", disk_read_total);
98 printf("Disk write operations : %li\n", disk_read_total);
99
100 /* Context switches */
101 file = fopen("/proc/stat", "r");
102 for (i = 0; i <= 2; i++)
103 while (fgetc(file) != '\n');
104 fgets(buffer, 5, file);
105 fgets(buffer, 50, file);
106 fclose(file);
107 printf("Kernel context switches: %s", buffer);
108
109 /* Time/Date last booted */
110 file = fopen("/proc/uptime", "r");
111 fscanf(file, "%li", &uptime);
112 fclose(file);
113 now = time(NULL);
114 boot_time = now - uptime;
115 printf("Time/Date last booted: %s", ctime(&boot_time));
116
117 /* Number of processes since boot */
118 file = fopen("/proc/stat", "r");
119 for (i = 0; i <= 4; i++)
120 while (fgetc(file) != '\n');
121 fgets(buffer, 10, file);
122 fgets(buffer, 50, file);
123 fclose(file);
124 printf("Processes since boot: %s", buffer);
125
126}
127
128void partDmem() {
129 long int mem_available, mem_total;
130 FILE * file;
131
132 file = fopen("/proc/meminfo", "r");
133 fscanf(file, "%*s %li %*s", &mem_total);
134 fscanf(file, "%*s %li %*s", &mem_available);
135 fclose(file);
136
137 printf("Memory total : %li kB\n", mem_total);
138 printf("Memory available : %li kB\n", mem_available);
139}
140
141void partDloadavg(const int times, const int sleeptime) {
142 float loadavg_min;
143 int i;
144 FILE * file;
145
146 printf("Sleep timer: %i sec\n", sleeptime);
147
148 for (i = 0; i < times; i++) {
149 /* Might want do some forking over here to fake some load, by
150 * now load needs to be generated by some external processes
151 */
152 sleep(sleeptime);
153
154 file = fopen("/proc/loadavg", "r");
155 fscanf(file, "%f", &loadavg_min);
156 fclose(file);
157 printf("%03i : %03.2f\n", i, loadavg_min);
158 }
159}
160
161int main(int argc, char * argv[]) {
162 int extendedinfoflag = 0;
163 int loadavgtimes = 0;
164 int loadavgsleep = 60;
165
166
167 int ch;
168 while ((ch = getopt(argc, argv, "hil:s:")) != -1)
169 switch (ch) {
170 case 'i':
171 extendedinfoflag = 1;
172 break;
173 case 'l':
174 loadavgtimes = atoi(optarg);
175 break;
176 case 's':
177 loadavgsleep = atoi(optarg);
178 break;
179 case 'h':
180 usage(EX_OK);
181 /* NOTREACHED */
182 default:
183 usage(EX_USAGE);
184 /* NOTREACHED */
185 }
186
187 partB();
188 if (extendedinfoflag == 1)
189 partC();
190
191 partDmem();
192 if (loadavgtimes > 0)
193 partDloadavg(loadavgtimes, loadavgsleep);
194
195
196 return(EX_OK);
197}
Note: See TracBrowser for help on using the repository browser.