bdcf45a50e266769aa0765a4f434de952922b4f6
[NeonServV5.git] / src / modules / global.mod / cmd_global_meminfo.c
1 /* cmd_global_meminfo.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "cmd_global.h"
19 #include "memoryInfo.h"
20
21 /*
22 * no arguments
23 */
24
25 CMD_BIND(global_cmd_meminfo) {
26     #ifndef ENABLE_MEMORY_DEBUG
27     reply(getTextBot(), user, "NS_MEMINFO_DISABLED");
28     #else
29     if(argc > 0) {
30         struct Table *table;
31         int elementcount = 0;
32         struct memoryInfoLines *element, *elements;
33         elements = getMemoryInfoLines(argv[0]);
34         for(element = elements; element; element = element->next) {
35             elementcount++;
36         }
37         table = table_init(4, elementcount+2, 0);
38         char *content[4];
39         content[0] = get_language_string(user, "NS_MEMINFO_LINE");
40         content[1] = get_language_string(user, "NS_MEMINFO_COUNT");
41         content[2] = get_language_string(user, "NS_MEMINFO_SIZE");
42         content[3] = get_language_string(user, "NS_MEMINFO_TOTAL");
43         table_add(table, content);
44         char lineBuf[20];
45         char countBuf[20];
46         char sizeBuf[20];
47         char totalBuf[50];
48         unsigned int total_allocations = 0;
49         unsigned int total_allocated = 0;
50         for(element = elements; element; element = element->next) {
51             sprintf(lineBuf, "%u", element->line);
52             content[0] = lineBuf;
53             sprintf(countBuf, "%u", element->allocations);
54             total_allocations += element->allocations;
55             content[1] = countBuf;
56             sprintf(sizeBuf, "%uB", element->allocate);
57             content[2] = sizeBuf;
58             sprintf(totalBuf, "%u (%.2f kB)", (element->allocate * element->allocations), ((float)(element->allocate * element->allocations) / 1024));
59             total_allocated += (element->allocate * element->allocations);
60             content[3] = totalBuf;
61             table_add(table, content);
62         }
63         content[0] = "Total";
64         sprintf(countBuf, "%u", total_allocations);
65         content[1] = countBuf;
66         content[2] = "*";
67         sprintf(sizeBuf, "%u (%.2f kB)", total_allocated, ((float)total_allocated / 1024));
68         content[3] = sizeBuf;
69         table_add(table, content);
70         char **table_lines = table_end(table);
71         int i;
72         for(i = 0; i < table->entrys; i++) {
73             reply(getTextBot(), user, table_lines[i]);
74         }
75         table_free(table);
76     } else {
77         struct Table *table;
78         int elementcount = 0;
79         struct memoryInfoFiles *element, *elements;
80         elements = getMemoryInfoFiles();
81         for(element = elements; element; element = element->next) {
82             elementcount++;
83         }
84         table = table_init(3, elementcount+2, 0);
85         char *content[3];
86         content[0] = get_language_string(user, "NS_MEMINFO_NAME");
87         content[1] = get_language_string(user, "NS_MEMINFO_COUNT");
88         content[2] = get_language_string(user, "NS_MEMINFO_SIZE");
89         table_add(table, content);
90         char countBuf[20];
91         char sizeBuf[50];
92         unsigned int total_allocations = 0;
93         unsigned int total_allocated = 0;
94         for(element = elements; element; element = element->next) {
95             content[0] = element->filename;
96             sprintf(countBuf, "%u", element->allocations);
97             total_allocations += element->allocations;
98             content[1] = countBuf;
99             sprintf(sizeBuf, "%u (%.2f kB)", element->allocated, ((float)element->allocated / 1024));
100             total_allocated += element->allocated;
101             content[2] = sizeBuf;
102             table_add(table, content);
103         }
104         content[0] = "Total";
105         sprintf(countBuf, "%u", total_allocations);
106         content[1] = countBuf;
107         sprintf(sizeBuf, "%u (%.2f kB)", total_allocated, ((float)total_allocated / 1024));
108         content[2] = sizeBuf;
109         table_add(table, content);
110         char **table_lines = table_end(table);
111         int i;
112         for(i = 0; i < table->entrys; i++) {
113             reply(getTextBot(), user, table_lines[i]);
114         }
115         table_free(table);
116     }
117     #endif
118 }