Lindent pass.
[meego-developer-tools:memuse.git] / report.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 Wong <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 reporting routines
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <linux/types.h>
26 #include <dirent.h>
27 #include <assert.h>
28 #include <string.h>
29
30 #include "memuse.h"
31
32 static int comparef(gconstpointer a, gconstpointer b)
33 {
34         struct program *A = (struct program *)a;
35         struct program *B = (struct program *)b;
36
37         return B->kb - A->kb;
38 }
39
40 struct program *find_program(char *name)
41 {
42         GList *item;
43         struct program *program;
44
45         item = g_list_first(programs_d);
46         while (item) {
47                 program = item->data;
48                 if (strncmp(program->name, name, strlen(name)) == 0)
49                         break;
50                 item = g_list_next(item);
51         }
52         if (!item)
53                 program = NULL;
54         return program;
55 }
56
57 void report_results(void)
58 {
59         GList *item;
60         uint64_t total = 0;
61         int count = 0;
62
63         struct program *program;
64
65         programs = g_list_sort(programs, comparef);
66         item = g_list_first(programs);
67         while (item) {
68                 char buf[256];
69                 struct program *program_d = NULL;
70                 if (num != 0 && count >= num)
71                         break;
72                 program = item->data;
73                 if (dfile)
74                         program_d = find_program(program->name);
75                 total += program->kb;
76                 if (program_d)
77                         sprintf(buf, _("%8lluKb(%+6lldK)\t%s \n"), program->kb,
78                                 program->kb - program_d->kb, program->name);
79                 else
80                         sprintf(buf, _("%8lluKb\t\t%s \n"), program->kb,
81                                 program->name);
82
83                 if (daem == 0)
84                         printf("%s", buf);
85
86                 if (ofile) {
87                         sprintf(buf, _("%8lluKb\t\t%s \n"), program->kb,
88                                 program->name);
89                         fputs(buf, ofile);
90                 }
91                 count++;
92                 item = g_list_next(item);
93         }
94
95         uint64_t prv_size = get_pvr_total() / 1024;
96
97         if (daem == 0) {
98                 printf(_("%8lluKb\t\tsystem total\n"), total);
99                 report_library();
100         } else {
101                 if (sfile) {
102                         char buf[256];
103                         fseek(sfile, 0, SEEK_SET);
104                         sprintf(buf, _("%lluKb %lluKb\n"), total, prv_size);
105                         fputs(buf, sfile);
106                 }
107         }
108
109 }