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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// SPDX-License-Identifier: MIT
/*
* Copyright © 2024 Intel Corporation
*/
#include <fcntl.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "drmtest.h"
#include "igt_power.h"
struct measurement {
int battery_index;
const char *rapl_domain;
const char *drm_device;
struct power_sample pre, post;
struct igt_power power;
};
static bool prepare(struct measurement *m)
{
if (m->battery_index >= 0) {
int ret;
ret = igt_power_bat_open(&m->power, m->battery_index);
if (ret) {
fprintf(stderr, "Unable to open battery %d (%d)\n",
m->battery_index, ret);
return false;
}
}
if (m->rapl_domain) {
int ret, fd = -1;
if (m->drm_device) {
fd = open(m->drm_device, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Unable to open drm device %s (%d)\n",
m->drm_device, -errno);
return false;
}
}
ret = igt_power_open(fd, &m->power, m->rapl_domain);
if (ret) {
if (m->drm_device)
fprintf(stderr, "Unable to open hwmon/rapl for %s (%d)\n",
m->drm_device, ret);
else
fprintf(stderr, "Unable to open rapl domain %s (%d)\n",
m->rapl_domain, ret);
close(fd);
return false;
}
close(fd);
}
return true;
}
static void sample_pre(struct measurement *m)
{
igt_power_get_energy(&m->power, &m->pre);
}
static void sample_post(struct measurement *m)
{
igt_power_get_energy(&m->power, &m->post);
}
static void report(struct measurement *m)
{
if (m->battery_index >= 0)
printf("battery[%d]: energy %f mJ, power %f mW, time %f s\n",
m->battery_index,
igt_power_get_mJ(&m->power, &m->pre, &m->post),
igt_power_get_mW(&m->power, &m->pre, &m->post),
igt_power_get_s(&m->pre, &m->post));
else
printf("%s[%s]: energy %f mJ, power %f mW, time %f s\n",
m->drm_device ?: "rapl", m->rapl_domain,
igt_power_get_mJ(&m->power, &m->pre, &m->post),
igt_power_get_mW(&m->power, &m->pre, &m->post),
igt_power_get_s(&m->pre, &m->post));
igt_power_close(&m->power);
}
static void __attribute__((noreturn)) usage(const char *name)
{
fprintf(stderr,
"Usage: %s [[-d <device>][-r <domain>][-b <battery>]...][-S <seconds>][-s <seconds>]\n"
" -d,--drm <device>\tDRM device (eg. /dev/dri/card0)\n"
" -r,--rapl <domain>\trapl domain (cpu,gpu,pkg,ram)\n"
" -b,--battery <battery>\tbattery index\n"
" -S,--settle <seconds>\tsettling duration\n"
" -s,--sleep <seconds>\tmeasurement duration\n",
name);
exit(1);
}
int main(int argc, char *argv[])
{
struct measurement measurements[8];
int num_measurements = 0;
int measurement_duration = 0;
int settle_duration = 0;
for (;;) {
static const struct option long_options[] = {
{ .name = "drm", .has_arg = required_argument, .val = 'd', },
{ .name = "rapl", .has_arg = required_argument, .val = 'r', },
{ .name = "battery", .has_arg = required_argument, .val = 'b', },
{ .name = "sleep", .has_arg = required_argument, .val = 's', },
{ .name = "settle", .has_arg = required_argument, .val = 'S', },
{}
};
int opt;
opt = getopt_long(argc, argv, "d:r:b:s:S:", long_options, NULL);
if (opt == -1)
break;
switch (opt) {
case 'd':
if (num_measurements >= ARRAY_SIZE(measurements))
usage(argv[0]);
measurements[num_measurements].battery_index = -1;
measurements[num_measurements].rapl_domain = "gpu";
measurements[num_measurements].drm_device = optarg;
num_measurements++;
break;
case 'r':
if (num_measurements >= ARRAY_SIZE(measurements))
usage(argv[0]);
measurements[num_measurements].battery_index = -1;
measurements[num_measurements].rapl_domain = optarg;
measurements[num_measurements].drm_device = NULL;
num_measurements++;
break;
case 'b':
if (num_measurements >= ARRAY_SIZE(measurements))
usage(argv[0]);
measurements[num_measurements].battery_index = atoi(optarg);
measurements[num_measurements].rapl_domain = NULL;
measurements[num_measurements].drm_device = NULL;
num_measurements++;
break;
case 's':
measurement_duration = atoi(optarg);
break;
case 'S':
settle_duration = atoi(optarg);
break;
default:
usage(argv[0]);
break;
}
}
if (num_measurements == 0)
usage(argv[0]);
for (int i = 0; i < num_measurements; i++) {
if (!prepare(&measurements[i]))
usage(argv[0]);
}
sleep(settle_duration);
for (int i = 0; i < num_measurements; i++)
sample_pre(&measurements[i]);
sleep(measurement_duration);
for (int i = 0; i < num_measurements; i++)
sample_post(&measurements[i]);
for (int i = 0; i < num_measurements; i++)
report(&measurements[i]);
return 0;
}
|