~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/intel_pstate/aperf.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <math.h>
  3 #include <unistd.h>
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <sys/types.h>
  7 #include <sys/stat.h>
  8 #include <fcntl.h>
  9 #include <sys/timeb.h>
 10 #include <sched.h>
 11 #include <errno.h>
 12 #include <string.h>
 13 #include <time.h>
 14 #include "../kselftest.h"
 15 
 16 #define MSEC_PER_SEC    1000L
 17 #define NSEC_PER_MSEC   1000000L
 18 
 19 void usage(char *name) {
 20         printf ("Usage: %s cpunum\n", name);
 21 }
 22 
 23 int main(int argc, char **argv) {
 24         unsigned int i, cpu, fd;
 25         char msr_file_name[64];
 26         long long tsc, old_tsc, new_tsc;
 27         long long aperf, old_aperf, new_aperf;
 28         long long mperf, old_mperf, new_mperf;
 29         struct timespec before, after;
 30         long long int start, finish, total;
 31         cpu_set_t cpuset;
 32 
 33         if (argc != 2) {
 34                 usage(argv[0]);
 35                 return 1;
 36         }
 37 
 38         errno = 0;
 39         cpu = strtol(argv[1], (char **) NULL, 10);
 40 
 41         if (errno) {
 42                 usage(argv[0]);
 43                 return 1;
 44         }
 45 
 46         sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
 47         fd = open(msr_file_name, O_RDONLY);
 48 
 49         if (fd == -1) {
 50                 printf("/dev/cpu/%d/msr: %s\n", cpu, strerror(errno));
 51                 return KSFT_SKIP;
 52         }
 53 
 54         CPU_ZERO(&cpuset);
 55         CPU_SET(cpu, &cpuset);
 56 
 57         if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset)) {
 58                 perror("Failed to set cpu affinity");
 59                 return 1;
 60         }
 61 
 62         if (clock_gettime(CLOCK_MONOTONIC, &before) < 0) {
 63                 perror("clock_gettime");
 64                 return 1;
 65         }
 66         pread(fd, &old_tsc,  sizeof(old_tsc), 0x10);
 67         pread(fd, &old_aperf,  sizeof(old_mperf), 0xe7);
 68         pread(fd, &old_mperf,  sizeof(old_aperf), 0xe8);
 69 
 70         for (i=0; i<0x8fffffff; i++) {
 71                 sqrt(i);
 72         }
 73 
 74         if (clock_gettime(CLOCK_MONOTONIC, &after) < 0) {
 75                 perror("clock_gettime");
 76                 return 1;
 77         }
 78         pread(fd, &new_tsc,  sizeof(new_tsc), 0x10);
 79         pread(fd, &new_aperf,  sizeof(new_mperf), 0xe7);
 80         pread(fd, &new_mperf,  sizeof(new_aperf), 0xe8);
 81 
 82         tsc = new_tsc-old_tsc;
 83         aperf = new_aperf-old_aperf;
 84         mperf = new_mperf-old_mperf;
 85 
 86         start = before.tv_sec*MSEC_PER_SEC + before.tv_nsec/NSEC_PER_MSEC;
 87         finish = after.tv_sec*MSEC_PER_SEC + after.tv_nsec/NSEC_PER_MSEC;
 88         total = finish - start;
 89 
 90         printf("runTime: %4.2f\n", 1.0*total/MSEC_PER_SEC);
 91         printf("freq: %7.0f\n", tsc / (1.0*aperf / (1.0 * mperf)) / total);
 92         return 0;
 93 }
 94 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php