File: getspeed.c

package info (click to toggle)
cpufreqd 2.4.2-3
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 624 kB
  • sloc: ansic: 5,484; makefile: 239; sh: 133
file content (133 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include "cpufreqd_remote.h"

static int cpufreqd_dirs(const struct dirent *d) {
	return (strncmp(d->d_name, "cpufreqd-", 9) == 0);
}

int main(int argc, char *argv[])
{
	int sock;
	struct dirent **namelist = NULL;
	struct sockaddr_un sck;
	struct stat st;
	time_t last_mtime = 0;
	unsigned int cmd = 0;
	unsigned int full_cmd = 0;
	unsigned int i = 0;
	char buf[4096] = {0}, name[256] = {0}, policy[255] = {0};
	char *in;
	int min, max, active, n;

	if (argc == 2 && !strcmp(argv[1], "-l"))
		cmd = CMD_CUR_PROFILES;
	else if (argc == 1)
		cmd = CMD_LIST_PROFILES;
	else {
		fprintf(stdout, "%s: Wrong arguments\n", argv[0]);
		fprintf(stdout, "%s [-l]\n", argv[0]);
		fprintf(stdout, "	-l  list applied profiles\n");
		return 1;
	}
	
	sck.sun_family = AF_UNIX;
	sck.sun_path[0] = '\0';
	/* get path */
	n = scandir("/tmp", &namelist, &cpufreqd_dirs, NULL);
	if (n > 0) { 
		while (n--) {
			snprintf(buf, 108, "/tmp/%s", namelist[n]->d_name);
			free(namelist[n]);
			if (stat(buf, &st) != 0) {
				fprintf(stderr, "%s: %s\n", buf, strerror(errno));
				continue;
			}
#if 0
			fprintf(stdout, "%s %lu %lu %lu\n", buf, st.st_ctime, st.st_atime, st.st_mtime);
#endif
			if (last_mtime == 0 || last_mtime < st.st_mtime) {
				last_mtime = st.st_mtime;
				snprintf(sck.sun_path, 108,"%s/cpufreqd", buf);
#if 0
				fprintf(stdout, "--> %s\n", buf);
#endif
			}
		}
		free(namelist);
	} else {
		fprintf(stderr, "No cpufreqd socket found\n");
		return ENOENT;
	}
	if (!sck.sun_path) {
		fprintf(stderr, "No cpufreqd socket found\n");
		return ENOENT;
	}
#if 0
	fprintf(stdout, "socket I'll try to connect: %s\n", sck.sun_path);
#endif

	if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
		perror("socket()");
		return 1;
	}

	if (connect(sock, (struct sockaddr *)&sck, sizeof(sck)) == -1) {
		perror("connect()");
		return 1;
	}
	
	full_cmd = MAKE_COMMAND(cmd, 0);
	if (write(sock, &full_cmd, 4) != 4)
		perror("write()");

	n = 0;
	while (read(sock, buf, 4096)) {
		int is_active = 0;
		in = buf;
		while (cmd == CMD_LIST_PROFILES 
				&& sscanf(in, "%d/%[^/]/%d/%d/%[^\n]\n", &active, name, &min, &max, policy) == 5) {
			printf("\nName (#%d):\t%s\n", ++n, name);
			/* pretty print active cpus */
			is_active = 0;
			for (i = 0; i < sizeof(active); i++) {
				if (active & (1 << i)) {
					if (is_active == 0)
						printf("Active on CPU#:\t");
					else if (is_active > 0)
						printf(", ");
					printf("%d", i);
					is_active++;
				}
			}
			if (is_active)
				printf("\n");

			printf("Governor:\t%s\n", policy);
			printf("Min freq:\t%d\n", min);
			printf("Max freq:\t%d\n", max);
			in = strchr(in, '\n') + 1;
		}

		while (cmd == CMD_CUR_PROFILES
				&& sscanf(in, "%d/%[^/]/%d/%d/%[^\n]\n", &active, name, &min, &max, policy) == 5) {
			printf("\nCPU#%d: \"%s\" ", active, name);
			printf("%s %d-%d\n", policy, min, max);
			in = strchr(in, '\n') + 1;
		}
	}

	close(sock);

	return 0;
}