Import: v0.21
[meego-developer-tools:memuse.git] / memuse.c
1 /*
2  * memuse - a tool to give a reasonable per application memory footprint estimate
3  *
4  * (C) Copyright 2008 Intel Corporation
5  *
6  * Authors: 
7  *      Arjan van de Ven <arjan@linux.intel.com>
8  *      Jing Wang <jing.j.wang@intel.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; version 2
13  * of the License.
14  *
15  * This file contains the main routines and program flow
16  */
17
18
19 #define _GNU_SOURCE 
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <linux/types.h>
28 #include <sys/wait.h>
29 #include <dirent.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <getopt.h>
33 #include <string.h>
34
35 #include "memuse.h"
36
37 GList *programs = NULL;
38 GList *programs_d = NULL;
39 FILE *dfile = NULL;
40 FILE *ofile = NULL;
41 FILE *sfile = NULL;
42 int daem = 0;
43 int tt = 0;
44 int num = 0;
45
46 void free_node(gpointer data, gpointer user_data)
47 {
48         struct program *p = (struct program *)data;
49         if (p)
50                 free(p);
51 }
52
53 void free_list(void)
54 {
55         g_list_foreach(programs, free_node, NULL);
56         g_list_foreach(programs_d, free_node, NULL);
57         g_list_free(programs);
58         g_list_free(programs_d);
59         programs = NULL;
60         programs_d = NULL;
61 }
62  
63 void cleanup(void)
64 {
65         free_list();
66         if (dfile)
67                 fclose(dfile);
68         if (ofile)
69                 fclose(ofile);
70         if (sfile)
71                 fclose(sfile);
72 }
73
74
75 void usage()
76 {
77         printf(_("Usage: memuse [OPTION...]\n"));
78         printf(_("  -o, --output=File            output the memory use list into specified file\n"));
79         printf(_("  -d, --diff=File              diff the prevous memory use case with current\n"));
80         printf(_("  -n, --num=Integer            Show top how many processes by memory use\n"));
81         printf(_("  -h, --help            Show this help message\n"));
82         exit(0);
83 }
84
85 int main(int argc, char **argv)
86 {
87         setlocale (LC_ALL, "");
88         bindtextdomain ("memuse", "/usr/share/locale");
89         textdomain ("memuse");
90
91         if (argc < 0 && argv[0] == NULL)
92                 return EXIT_FAILURE; /* shut up compiler warning */
93
94         while (1) {
95                 static struct option opts[] = {
96                         { "output", 1, NULL, 'o' },
97                         { "diff", 1, NULL, 'i' },
98                         { "num", 1, NULL, 'n' },
99                         { "time", 1, NULL, 't'}, 
100                         { "file", 1, NULL, 'f'},
101                         { "daemon", 0, NULL, 'd'},
102                         { "help", 0, NULL, 'h' },
103                         { 0, 0, NULL, 0 }
104                 };
105                 int index2 = 0, c;
106
107                 c = getopt_long(argc, argv, "o:i:n:t:f:dh", opts, &index2);
108                 if (c == -1)
109                         break;
110                 switch (c) {
111                         case 'o':
112                                 ofile = fopen(optarg,"w+");
113                                 if ( !ofile){
114                                         fprintf(stderr,_("Fail to create or open %s!"),optarg);
115                                         return EXIT_FAILURE;
116                                 }       
117                                 break;
118
119                         case 'i':
120                                 dfile = fopen(optarg,"r");
121                                 if ( !dfile){
122                                         fprintf(stderr,_("Fail to create or open %s!"),optarg);
123                                         return EXIT_FAILURE;
124                                 }       
125                                 break;
126
127                         case 'n':
128                                 num = atoi(optarg);
129                                 break;
130
131                         case 't':
132                                 tt = atoi(optarg);
133                                 break;
134
135                         case 'f':
136                                 sfile = fopen(optarg, "w+");
137                                 break;
138
139                         case 'd':
140                                 printf("Use daemon mode\n");
141                                 daem = 1;
142                                 break;
143
144                         case 'h':
145                                 usage();
146                                 break;
147
148                         default:
149                                 ;
150                 }
151         }
152
153         if (dfile)
154                 parse_savedfile();
155
156         if (daem) {     
157                 pid_t pid, sid;
158                 pid = fork();
159                 if (pid > 0)
160                         exit(0);
161                 else {
162                         if ((sid=setsid())<0)
163                                 exit(1);
164                         umask(0);
165                         while (1) {
166                                 parse_proc();
167                                 report_results();
168                                 free_list();
169                                 sleep(tt);      
170                         }
171                 }
172         }else{
173                 parse_proc();
174                 report_results();
175         }
176         cleanup();
177         return 0;
178 }