blob: b5286f0cf8c6cf844975e6da52e3b00c494da5b8 [file] [log] [blame]
Chris Wilsonb98bade2013-08-20 21:39:27 +01001/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
Chris Wilsonbaa5be02013-08-20 09:27:34 +010025#include <sys/stat.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <time.h>
32#include <errno.h>
33
Tvrtko Ursulina688dec2017-09-13 17:38:16 +010034#include "igt_perf.h"
35
Chris Wilsonbaa5be02013-08-20 09:27:34 +010036#include "rc6.h"
Chris Wilson65d24812013-08-27 15:19:58 +010037
Chris Wilsonbaa5be02013-08-20 09:27:34 +010038int rc6_init(struct rc6 *rc6)
39{
Chris Wilsonbaa5be02013-08-20 09:27:34 +010040 memset(rc6, 0, sizeof(*rc6));
41
Tvrtko Ursulin4c57ff42017-11-24 17:16:18 +000042 rc6->fd = perf_i915_open(I915_PMU_RC6_RESIDENCY);
43 if (rc6->fd < 0) {
Chris Wilson65d24812013-08-27 15:19:58 +010044 struct stat st;
45 if (stat("/sys/class/drm/card0/power", &st) < 0)
46 return rc6->error = errno;
47 }
Chris Wilsonbaa5be02013-08-20 09:27:34 +010048
49 return 0;
50}
51
52static uint64_t file_to_u64(const char *path)
53{
54 char buf[4096];
55 int fd, len;
56
57 fd = open(path, 0);
58 if (fd < 0)
Chris Wilson65d24812013-08-27 15:19:58 +010059 return -1;
Chris Wilsonbaa5be02013-08-20 09:27:34 +010060
61 len = read(fd, buf, sizeof(buf)-1);
62 close(fd);
63
64 if (len < 0)
Chris Wilson65d24812013-08-27 15:19:58 +010065 return -1;
Chris Wilsonbaa5be02013-08-20 09:27:34 +010066
67 buf[len] = '\0';
68
69 return strtoull(buf, 0, 0);
70}
71
72static uint64_t clock_ms_to_u64(void)
73{
74 struct timespec tv;
75
76 if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0)
77 return 0;
78
Chris Wilsoncf62d522013-08-20 10:58:02 +010079 return (uint64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000;
Chris Wilsonbaa5be02013-08-20 09:27:34 +010080}
81
82int rc6_update(struct rc6 *rc6)
83{
84 struct rc6_stat *s = &rc6->stat[rc6->count++&1];
85 struct rc6_stat *d = &rc6->stat[rc6->count&1];
86 uint64_t d_time, d_rc6, d_rc6p, d_rc6pp;
Chris Wilsonbaa5be02013-08-20 09:27:34 +010087
88 if (rc6->error)
89 return rc6->error;
90
Tvrtko Ursulin4c57ff42017-11-24 17:16:18 +000091 if (rc6->fd < 0) {
Chris Wilson65d24812013-08-27 15:19:58 +010092 struct stat st;
Chris Wilsonc8885072013-08-20 11:08:13 +010093
Chris Wilson65d24812013-08-27 15:19:58 +010094 if (stat("/sys/class/drm/card0/power/rc6_residency_ms", &st) < 0)
95 return rc6->error = ENOENT;
Chris Wilsonbaa5be02013-08-20 09:27:34 +010096
Chris Wilson65d24812013-08-27 15:19:58 +010097 s->rc6_residency = file_to_u64("/sys/class/drm/card0/power/rc6_residency_ms");
98 s->rc6p_residency = file_to_u64("/sys/class/drm/card0/power/rc6p_residency_ms");
99 s->rc6pp_residency = file_to_u64("/sys/class/drm/card0/power/rc6pp_residency_ms");
100 s->timestamp = clock_ms_to_u64();
101 } else {
Tvrtko Ursulin4c57ff42017-11-24 17:16:18 +0000102 uint64_t data[2];
Chris Wilson65d24812013-08-27 15:19:58 +0100103
Tvrtko Ursulin4c57ff42017-11-24 17:16:18 +0000104 if (read(rc6->fd, data, sizeof(data)) < sizeof(data))
Chris Wilson65d24812013-08-27 15:19:58 +0100105 return rc6->error = errno;
106
Tvrtko Ursulin4c57ff42017-11-24 17:16:18 +0000107 s->timestamp = data[1] / 1e6;
108 s->rc6_residency = data[0] / 1e6;
Chris Wilson65d24812013-08-27 15:19:58 +0100109 }
Chris Wilsonbaa5be02013-08-20 09:27:34 +0100110
111 if (rc6->count == 1)
112 return EAGAIN;
113
114 d_time = s->timestamp - d->timestamp;
Chris Wilson65d24812013-08-27 15:19:58 +0100115 if (d_time == 0) {
116 rc6->count--;
117 return EAGAIN;
118 }
Chris Wilsonbaa5be02013-08-20 09:27:34 +0100119
120 d_rc6 = s->rc6_residency - d->rc6_residency;
Tvrtko Ursulin0d8385a2017-09-13 18:28:24 +0100121 rc6->rc6 = 100 * d_rc6 / d_time;
Chris Wilsonbaa5be02013-08-20 09:27:34 +0100122
123 d_rc6p = s->rc6p_residency - d->rc6p_residency;
Tvrtko Ursulin0d8385a2017-09-13 18:28:24 +0100124 rc6->rc6p = 100 * d_rc6p / d_time;
Chris Wilsonbaa5be02013-08-20 09:27:34 +0100125
126 d_rc6pp = s->rc6pp_residency - d->rc6pp_residency;
Tvrtko Ursulin0d8385a2017-09-13 18:28:24 +0100127 rc6->rc6pp = 100 * d_rc6pp / d_time;
Chris Wilsonbaa5be02013-08-20 09:27:34 +0100128
Tvrtko Ursulin0d8385a2017-09-13 18:28:24 +0100129 rc6->rc6_combined = 100 * (d_rc6 + d_rc6p + d_rc6pp) / d_time;
Chris Wilsonbaa5be02013-08-20 09:27:34 +0100130 return 0;
131}