1 /*
2  *  (C) 2010,2011       Thomas Renninger <trenn@suse.de>, Novell Inc.
3  *
4  *  Licensed under the terms of the GNU GPL License version 2.
5  *
6  */
7 
8 #ifndef __CPUIDLE_INFO_HW__
9 #define __CPUIDLE_INFO_HW__
10 
11 #include <stdarg.h>
12 #include <time.h>
13 
14 #include "idle_monitor/idle_monitors.h"
15 
16 #define MONITORS_MAX 20
17 #define MONITOR_NAME_LEN 20
18 
19 /* CSTATE_NAME_LEN is limited by header field width defined
20  * in cpupower-monitor.c. Header field width is defined to be
21  * sum of percent width and two spaces for padding.
22  */
23 #ifdef __powerpc__
24 #define CSTATE_NAME_LEN 7
25 #else
26 #define CSTATE_NAME_LEN 5
27 #endif
28 #define CSTATE_DESC_LEN 60
29 
30 int cpu_count;
31 
32 /* Hard to define the right names ...: */
33 enum power_range_e {
34 	RANGE_THREAD,	/* Lowest in topology hierarcy, AMD: core, Intel: thread
35 			   kernel sysfs: cpu */
36 	RANGE_CORE,	/* AMD: unit, Intel: core, kernel_sysfs: core_id */
37 	RANGE_PACKAGE,	/* Package, processor socket */
38 	RANGE_MACHINE,	/* Machine, platform wide */
39 	RANGE_MAX };
40 
41 typedef struct cstate {
42 	int  id;
43 	enum power_range_e range;
44 	char name[CSTATE_NAME_LEN];
45 	char desc[CSTATE_DESC_LEN];
46 
47 	/* either provide a percentage or a general count */
48 	int (*get_count_percent)(unsigned int self_id, double *percent,
49 				 unsigned int cpu);
50 	int (*get_count)(unsigned int self_id, unsigned long long *count,
51 			 unsigned int cpu);
52 } cstate_t;
53 
54 struct cpuidle_monitor {
55 	/* Name must not contain whitespaces */
56 	char name[MONITOR_NAME_LEN];
57 	int name_len;
58 	int hw_states_num;
59 	cstate_t *hw_states;
60 	int (*start) (void);
61 	int (*stop) (void);
62 	struct cpuidle_monitor* (*do_register) (void);
63 	void (*unregister)(void);
64 	unsigned int overflow_s;
65 	int needs_root;
66 };
67 
68 extern long long timespec_diff_us(struct timespec start, struct timespec end);
69 
70 #define print_overflow_err(mes, ov)						\
71 {										\
72 	fprintf(stderr, gettext("Measure took %u seconds, but registers could "	\
73 				"overflow at %u seconds, results "		\
74 				"could be inaccurate\n"), mes, ov);		\
75 }
76 
77 
78 /* Taken over from x86info project sources  -> return 0 on success */
79 #include <sched.h>
80 #include <sys/types.h>
81 #include <unistd.h>
bind_cpu(int cpu)82 static inline int bind_cpu(int cpu)
83 {
84 	cpu_set_t set;
85 
86 	if (sched_getaffinity(getpid(), sizeof(set), &set) == 0) {
87 		CPU_ZERO(&set);
88 		CPU_SET(cpu, &set);
89 		return sched_setaffinity(getpid(), sizeof(set), &set);
90 	}
91 	return 1;
92 }
93 
94 #endif /* __CPUIDLE_INFO_HW__ */
95