tizen 2.3 release
[kernel/api/system-resource.git] / src / utils / proc-stat-test.c
1 /*
2  * resourced
3  *
4  * Lib for getting process statistics
5  *
6  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <proc_stat.h>
26
27
28
29 int main(void)
30 {
31         GArray *valid_proc_infos = NULL;
32         GArray *terminated_proc_infos = NULL;
33         proc_stat_system_time st_diff;
34
35         terminated_proc_infos = g_array_new(false, false, sizeof(proc_stat_process_info));
36         valid_proc_infos = g_array_new(false, false, sizeof(proc_stat_process_info));
37
38
39         proc_stat_init();
40
41         while (true) {
42
43                 proc_stat_get_process_info(valid_proc_infos, terminated_proc_infos, NULL);
44                 proc_stat_get_system_time_diff(&st_diff);
45
46                 if (st_diff.total_time != 0) {
47
48                         double total_time = st_diff.total_time;
49
50                         printf("Total CPU Info : %3.2lf%%us %3.2lf%%sy %3.2lf%%ni %3.2lf%%id %3.2lf%%iowait %3.2lf%%irq %3.2lf%%softirq\n",
51                                 (double)st_diff.user_time / total_time * 100,
52                                 (double)st_diff.system_time / total_time * 100,
53                                 (double)st_diff.nice_time / total_time * 100,
54                                 (double)st_diff.idle_time / total_time * 100,
55                                 (double)st_diff.iowait_time / total_time * 100,
56                                 (double)st_diff.irq_time / total_time * 100,
57                                 (double)st_diff.softirq_time / total_time * 100);
58
59                         unsigned int total, free;
60                         if (proc_stat_get_total_mem_size(&total) && proc_stat_get_free_mem_size(&free))
61                                 printf("Total Memory Info : Total:%dMB Free:%dMB Used:%dMB\n", total, free, total - free);
62
63                         unsigned int i = 0;
64                         for (i = 0; i < valid_proc_infos->len; ++i) {
65                                 proc_stat_process_info *ps = &g_array_index(valid_proc_infos, proc_stat_process_info, i);
66
67                                 if ((ps->active) || (ps->fresh)) {
68                                         if (ps->fresh)
69                                                 printf("N ");
70                                         else
71                                                 printf("  ");
72
73                                         printf("[pid:%d\t name:%40s utime:%3.2lf%% stime:%3.2lf%% rss:%dKb\n",
74                                                 ps->pid, ps->name,
75                                                 (double)(ps->utime_diff)/(double)st_diff.total_time*100,
76                                                 (double)(ps->stime_diff)/(double)st_diff.total_time*100,
77                                                 ps->rss);
78                                 }
79                         }
80
81                         for (i = 0; i < terminated_proc_infos->len; ++i) {
82
83                                 proc_stat_process_info *ps = &g_array_index(terminated_proc_infos, proc_stat_process_info, i);
84
85                                 printf("T ");
86                                 printf("[pid:%d\t name:%40s\n",
87                                                 ps->pid, ps->name);
88                         }
89
90                 }
91
92                 usleep(1000000);
93                 g_array_set_size(valid_proc_infos, 0);
94                 g_array_set_size(terminated_proc_infos, 0);
95
96                 printf("-------------------------------------------------------------------------------\n");
97
98         }
99
100         if (valid_proc_infos) {
101                 g_array_free(valid_proc_infos, true);
102                 valid_proc_infos = NULL;
103         }
104
105
106         if (terminated_proc_infos) {
107                 g_array_free(terminated_proc_infos, true);
108                 terminated_proc_infos = NULL;
109         }
110
111         proc_stat_finalize();
112
113         return 0;
114 }