[IMPROVE] x86: apply jumper for US probes installing
[kernel/swap-modules.git] / energy / debugfs_energy.c
1 /*
2  *  Dynamic Binary Instrumentation Module based on KProbes
3  *  energy/debugfs_energy.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
22  *
23  */
24
25
26 #include <linux/fs.h>
27 #include <linux/module.h>
28 #include <linux/debugfs.h>
29 #include <linux/math64.h>
30 #include <driver/swap_debugfs.h>
31 #include "energy.h"
32 #include "rational_debugfs.h"
33 #include "lcd/lcd_debugfs.h"
34
35
36 /* CPU running */
37 static DEFINE_RATIONAL(cpu0_running_coef); /* boot core uses distinct coeff */
38 static DEFINE_RATIONAL(cpuN_running_coef);
39
40 static u64 __energy_cpu(enum parameter_energy pe)
41 {
42         u64 times[NR_CPUS] = { 0 };
43         u64 val = 0;
44         int i;
45
46         if (get_parameter_energy(pe, times, sizeof(times)) == 0) {
47                 val = div_u64(times[0] * cpu0_running_coef.num,
48                               cpu0_running_coef.denom);
49                 for (i = 1; i < NR_CPUS; i++)
50                         val += div_u64(times[i] * cpuN_running_coef.num,
51                                        cpuN_running_coef.denom);
52         }
53
54         return val;
55 }
56
57 static u64 cpu_system(void)
58 {
59         return __energy_cpu(PE_TIME_SYSTEM);
60 }
61
62 static u64 cpu_apps(void)
63 {
64         return __energy_cpu(PE_TIME_APPS);
65 }
66
67
68 /* CPU idle */
69 static DEFINE_RATIONAL(cpu_idle_coef);
70
71 static u64 cpu_idle_system(void)
72 {
73         u64 time = 0;
74
75         get_parameter_energy(PE_TIME_IDLE, &time, sizeof(time));
76         return div_u64(time * cpu_idle_coef.num, cpu_idle_coef.denom);
77 }
78
79
80 /* flash read */
81 static DEFINE_RATIONAL(fr_coef);
82
83 static u64 fr_system(void)
84 {
85         u64 byte = 0;
86
87         get_parameter_energy(PE_READ_SYSTEM, &byte, sizeof(byte));
88         return div_u64(byte * fr_coef.num, fr_coef.denom);
89 }
90
91 static u64 fr_apps(void)
92 {
93         u64 byte = 0;
94
95         get_parameter_energy(PE_READ_APPS, &byte, sizeof(byte));
96         return div_u64(byte * fr_coef.num, fr_coef.denom);
97 }
98
99
100 /* flash write */
101 static DEFINE_RATIONAL(fw_coef);
102
103 static u64 fw_system(void)
104 {
105         u64 byte = 0;
106
107         get_parameter_energy(PE_WRITE_SYSTEM, &byte, sizeof(byte));
108         return div_u64(byte * fw_coef.num, fw_coef.denom);
109 }
110
111 static u64 fw_apps(void)
112 {
113         u64 byte = 0;
114
115         get_parameter_energy(PE_WRITE_APPS, &byte, sizeof(byte));
116         return div_u64(byte * fw_coef.num, fw_coef.denom);
117 }
118
119
120
121
122
123 /* ============================================================================
124  * ===                             PARAMETERS                               ===
125  * ============================================================================
126  */
127 static int get_func_u64(void *data, u64 *val)
128 {
129         u64 (*func)(void) = data;
130         *val = func();
131         return 0;
132 }
133
134 DEFINE_SIMPLE_ATTRIBUTE(fops_get_u64, get_func_u64, NULL, "%llu\n");
135
136
137 struct param_data {
138         char *name;
139         struct rational *coef;
140         u64 (*system)(void);
141         u64 (*apps)(void);
142 };
143
144 static struct dentry *create_parameter(struct dentry *parent,
145                                        struct param_data *param)
146 {
147         struct dentry *name, *system, *apps = NULL;
148
149         name = debugfs_create_dir(param->name, parent);
150         if (name == NULL)
151                 return NULL;
152
153         system = debugfs_create_file("system", 0600, name, param->system,
154                                      &fops_get_u64);
155         if (system == NULL)
156                 goto rm_name;
157
158         if (param->apps) {
159                 apps = debugfs_create_file("apps", 0600, name, param->apps,
160                                            &fops_get_u64);
161                 if (apps == NULL)
162                         goto rm_system;
163         }
164
165         if (create_rational_files(name, param->coef,
166                                   "numerator", "denominator"))
167                 goto rm_apps;
168
169         return name;
170
171 rm_apps:
172         if (param->apps)
173                 debugfs_remove(apps);
174 rm_system:
175         debugfs_remove(system);
176 rm_name:
177         debugfs_remove(name);
178
179         return NULL;
180 }
181
182 struct param_data parameters[] = {
183         {
184                 .name = "cpu_running",
185                 .coef = &cpu0_running_coef,
186                 .system = cpu_system,
187                 .apps = cpu_apps
188         },
189         {
190                 .name = "cpuN_running",
191                 .coef = &cpuN_running_coef,
192                 .system = cpu_system,
193                 .apps = cpu_apps
194         },
195         {
196                 .name = "cpu_idle",
197                 .coef = &cpu_idle_coef,
198                 .system = cpu_idle_system,
199                 .apps = NULL
200         },
201         {
202                 .name = "flash_read",
203                 .coef = &fr_coef,
204                 .system = fr_system,
205                 .apps = fr_apps
206         },
207         {
208                 .name = "flash_write",
209                 .coef = &fw_coef,
210                 .system = fw_system,
211                 .apps = fw_apps
212         }
213 };
214
215 enum {
216         parameters_cnt = sizeof(parameters) / sizeof(struct param_data)
217 };
218
219
220
221
222
223 /* ============================================================================
224  * ===                              INIT/EXIT                               ===
225  * ============================================================================
226  */
227 static struct dentry *energy_dir = NULL;
228
229 /**
230  * @brief Destroy debugfs for LCD
231  *
232  * @return Dentry of energy debugfs
233  */
234 struct dentry *get_energy_dir(void)
235 {
236         return energy_dir;
237 }
238
239 /**
240  * @brief Destroy debugfs for energy
241  *
242  * @return Void
243  */
244 void exit_debugfs_energy(void)
245 {
246         if (energy_dir)
247                 debugfs_remove_recursive(energy_dir);
248
249         energy_dir = NULL;
250 }
251
252 /**
253  * @brief Create debugfs for energy
254  *
255  * @return Error code
256  */
257 int init_debugfs_energy(void)
258 {
259         int i;
260         struct dentry *swap_dir, *dentry;
261
262         swap_dir = get_swap_debugfs_dir();
263         if (swap_dir == NULL)
264                 return -ENOENT;
265
266         energy_dir = debugfs_create_dir("energy", swap_dir);
267         if (energy_dir == NULL)
268                 return -ENOMEM;
269
270         for (i = 0; i < parameters_cnt; ++i) {
271                 dentry = create_parameter(energy_dir, &parameters[i]);
272                 if (dentry == NULL)
273                         goto fail;
274         }
275
276         return 0;
277
278 fail:
279         exit_debugfs_energy();
280         return -ENOMEM;
281 }