File Index
Files and directories
Sign in
FILE

workload_hint_test.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/thermal/intel/workload_hint/workload_hint_test.c C
  1// SPDX-License-Identifier: GPL-2.0
  2
  3#define _GNU_SOURCE
  4
  5#include <stdio.h>
  6#include <string.h>
  7#include <stdlib.h>
  8#include <unistd.h>
  9#include <fcntl.h>
 10#include <poll.h>
 11#include <signal.h>
 12
 13#define WORKLOAD_NOTIFICATION_DELAY_ATTRIBUTE "/sys/bus/pci/devices/0000:00:04.0/workload_hint/notification_delay_ms"
 14#define WORKLOAD_ENABLE_ATTRIBUTE "/sys/bus/pci/devices/0000:00:04.0/workload_hint/workload_hint_enable"
 15#define WORKLOAD_TYPE_INDEX_ATTRIBUTE  "/sys/bus/pci/devices/0000:00:04.0/workload_hint/workload_type_index"
 16
 17static const char * const workload_types[] = {
 18	"idle",
 19	"battery_life",
 20	"sustained",
 21	"bursty",
 22	NULL
 23};
 24
 25#define WORKLOAD_TYPE_MAX_INDEX	3
 26
 27void workload_hint_exit(int signum)
 28{
 29	int fd;
 30
 31	/* Disable feature via sysfs knob */
 32
 33	fd = open(WORKLOAD_ENABLE_ATTRIBUTE, O_RDWR);
 34	if (fd < 0) {
 35		perror("Unable to open workload type feature enable file");
 36		exit(1);
 37	}
 38
 39	if (write(fd, "0\n", 2) < 0) {
 40		perror("Can't disable workload hints");
 41		exit(1);
 42	}
 43
 44	printf("Disabled workload type prediction\n");
 45
 46	close(fd);
 47}
 48
 49int main(int argc, char **argv)
 50{
 51	struct pollfd ufd;
 52	char index_str[4];
 53	int fd, ret, index;
 54	char delay_str[64];
 55	int delay = 0;
 56
 57	printf("Usage: workload_hint_test [notification delay in milli seconds]\n");
 58
 59	if (argc > 1) {
 60		ret = sscanf(argv[1], "%d", &delay);
 61		if (ret < 0) {
 62			printf("Invalid delay\n");
 63			exit(1);
 64		}
 65
 66		printf("Setting notification delay to %d ms\n", delay);
 67		if (delay < 0)
 68			exit(1);
 69
 70		sprintf(delay_str, "%s\n", argv[1]);
 71		fd = open(WORKLOAD_NOTIFICATION_DELAY_ATTRIBUTE, O_RDWR);
 72		if (fd < 0) {
 73			perror("Unable to open workload notification delay");
 74			exit(1);
 75		}
 76
 77		if (write(fd, delay_str, strlen(delay_str)) < 0) {
 78			perror("Can't set delay");
 79			exit(1);
 80		}
 81
 82		close(fd);
 83	}
 84
 85	if (signal(SIGINT, workload_hint_exit) == SIG_IGN)
 86		signal(SIGINT, SIG_IGN);
 87	if (signal(SIGHUP, workload_hint_exit) == SIG_IGN)
 88		signal(SIGHUP, SIG_IGN);
 89	if (signal(SIGTERM, workload_hint_exit) == SIG_IGN)
 90		signal(SIGTERM, SIG_IGN);
 91
 92	/* Enable feature via sysfs knob */
 93	fd = open(WORKLOAD_ENABLE_ATTRIBUTE, O_RDWR);
 94	if (fd < 0) {
 95		perror("Unable to open workload type feature enable file");
 96		exit(1);
 97	}
 98
 99	if (write(fd, "1\n", 2) < 0) {
100		perror("Can't enable workload hints");
101		exit(1);
102	}
103
104	close(fd);
105
106	printf("Enabled workload type prediction\n");
107
108	while (1) {
109		fd = open(WORKLOAD_TYPE_INDEX_ATTRIBUTE, O_RDONLY);
110		if (fd < 0) {
111			perror("Unable to open workload type file");
112			exit(1);
113		}
114
115		if ((lseek(fd, 0L, SEEK_SET)) < 0) {
116			fprintf(stderr, "Failed to set pointer to beginning\n");
117			exit(1);
118		}
119
120		if (read(fd, index_str, sizeof(index_str)) < 0) {
121			fprintf(stderr, "Failed to read from:%s\n",
122			WORKLOAD_TYPE_INDEX_ATTRIBUTE);
123			exit(1);
124		}
125
126		ufd.fd = fd;
127		ufd.events = POLLPRI;
128
129		ret = poll(&ufd, 1, -1);
130		if (ret < 0) {
131			perror("poll error");
132			exit(1);
133		} else if (ret == 0) {
134			printf("Poll Timeout\n");
135		} else {
136			if ((lseek(fd, 0L, SEEK_SET)) < 0) {
137				fprintf(stderr, "Failed to set pointer to beginning\n");
138				exit(1);
139			}
140
141			if (read(fd, index_str, sizeof(index_str)) < 0)
142				exit(0);
143
144			ret = sscanf(index_str, "%d", &index);
145			if (ret < 0)
146				break;
147
148			index &= 0x0f;
149			if (index > WORKLOAD_TYPE_MAX_INDEX)
150				printf("Invalid workload type index\n");
151			else
152				printf("workload type:%s\n", workload_types[index]);
153		}
154
155		close(fd);
156	}
157}
~/ Read-only file preview.