Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2024 HiSilicon Limited
3 : : */
4 : :
5 : : #include <errno.h>
6 : : #include <stdlib.h>
7 : : #include <string.h>
8 : :
9 : : #include <eal_export.h>
10 : : #include <rte_lcore.h>
11 : : #include <rte_log.h>
12 : :
13 : : #include "power_common.h"
14 : : #include "rte_power_qos.h"
15 : :
16 : : #define PM_QOS_SYSFILE_RESUME_LATENCY_US \
17 : : "/sys/devices/system/cpu/cpu%u/power/pm_qos_resume_latency_us"
18 : :
19 : : #define PM_QOS_CPU_RESUME_LATENCY_BUF_LEN 32
20 : :
21 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_power_qos_set_cpu_resume_latency, 24.11)
22 : : int
23 : 0 : rte_power_qos_set_cpu_resume_latency(uint16_t lcore_id, int latency)
24 : : {
25 : : char buf[PM_QOS_CPU_RESUME_LATENCY_BUF_LEN];
26 : : uint32_t cpu_id;
27 : : FILE *f;
28 : : int ret;
29 : :
30 [ # # ]: 0 : if (!rte_lcore_is_enabled(lcore_id)) {
31 : 0 : POWER_LOG(ERR, "lcore id %u is not enabled", lcore_id);
32 : 0 : return -EINVAL;
33 : : }
34 : 0 : ret = power_get_lcore_mapped_cpu_id(lcore_id, &cpu_id);
35 [ # # ]: 0 : if (ret != 0)
36 : : return ret;
37 : :
38 [ # # ]: 0 : if (latency < 0) {
39 : 0 : POWER_LOG(ERR, "latency should be greater than and equal to 0");
40 : 0 : return -EINVAL;
41 : : }
42 : :
43 : 0 : ret = open_core_sysfs_file(&f, "w", PM_QOS_SYSFILE_RESUME_LATENCY_US, cpu_id);
44 [ # # ]: 0 : if (ret != 0) {
45 : 0 : POWER_LOG(ERR, "Failed to open "PM_QOS_SYSFILE_RESUME_LATENCY_US" : %s",
46 : : cpu_id, strerror(errno));
47 : 0 : return ret;
48 : : }
49 : :
50 : : /*
51 : : * Based on the sysfs interface pm_qos_resume_latency_us under
52 : : * @PM_QOS_SYSFILE_RESUME_LATENCY_US directory in kernel, their meaning
53 : : * is as follows for different input string.
54 : : * 1> the resume latency is 0 if the input is "n/a".
55 : : * 2> the resume latency is no constraint if the input is "0".
56 : : * 3> the resume latency is the actual value to be set.
57 : : */
58 [ # # ]: 0 : if (latency == RTE_POWER_QOS_STRICT_LATENCY_VALUE)
59 : : snprintf(buf, sizeof(buf), "%s", "n/a");
60 [ # # ]: 0 : else if (latency == RTE_POWER_QOS_RESUME_LATENCY_NO_CONSTRAINT)
61 : : snprintf(buf, sizeof(buf), "%u", 0);
62 : : else
63 : : snprintf(buf, sizeof(buf), "%u", latency);
64 : :
65 : 0 : ret = write_core_sysfs_s(f, buf);
66 [ # # ]: 0 : if (ret != 0)
67 : 0 : POWER_LOG(ERR, "Failed to write "PM_QOS_SYSFILE_RESUME_LATENCY_US" : %s",
68 : : cpu_id, strerror(errno));
69 : :
70 : 0 : fclose(f);
71 : :
72 : 0 : return ret;
73 : : }
74 : :
75 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_power_qos_get_cpu_resume_latency, 24.11)
76 : : int
77 : 0 : rte_power_qos_get_cpu_resume_latency(uint16_t lcore_id)
78 : : {
79 : : char buf[PM_QOS_CPU_RESUME_LATENCY_BUF_LEN];
80 : : int latency = -1;
81 : : uint32_t cpu_id;
82 : : FILE *f;
83 : : int ret;
84 : :
85 [ # # ]: 0 : if (!rte_lcore_is_enabled(lcore_id)) {
86 : 0 : POWER_LOG(ERR, "lcore id %u is not enabled", lcore_id);
87 : 0 : return -EINVAL;
88 : : }
89 : 0 : ret = power_get_lcore_mapped_cpu_id(lcore_id, &cpu_id);
90 [ # # ]: 0 : if (ret != 0)
91 : : return ret;
92 : :
93 : 0 : ret = open_core_sysfs_file(&f, "r", PM_QOS_SYSFILE_RESUME_LATENCY_US, cpu_id);
94 [ # # ]: 0 : if (ret != 0) {
95 : 0 : POWER_LOG(ERR, "Failed to open "PM_QOS_SYSFILE_RESUME_LATENCY_US" : %s",
96 : : cpu_id, strerror(errno));
97 : 0 : return ret;
98 : : }
99 : :
100 : 0 : ret = read_core_sysfs_s(f, buf, sizeof(buf));
101 [ # # ]: 0 : if (ret != 0) {
102 : 0 : POWER_LOG(ERR, "Failed to read "PM_QOS_SYSFILE_RESUME_LATENCY_US" : %s",
103 : : cpu_id, strerror(errno));
104 : 0 : goto out;
105 : : }
106 : :
107 : : /*
108 : : * Based on the sysfs interface pm_qos_resume_latency_us under
109 : : * @PM_QOS_SYSFILE_RESUME_LATENCY_US directory in kernel, their meaning
110 : : * is as follows for different output string.
111 : : * 1> the resume latency is 0 if the output is "n/a".
112 : : * 2> the resume latency is no constraint if the output is "0".
113 : : * 3> the resume latency is the actual value in used for other string.
114 : : */
115 [ # # ]: 0 : if (strcmp(buf, "n/a") == 0)
116 : : latency = RTE_POWER_QOS_STRICT_LATENCY_VALUE;
117 : : else {
118 : 0 : latency = strtoul(buf, NULL, 10);
119 [ # # ]: 0 : latency = latency == 0 ? RTE_POWER_QOS_RESUME_LATENCY_NO_CONSTRAINT : latency;
120 : : }
121 : :
122 : 0 : out:
123 : 0 : fclose(f);
124 : :
125 [ # # ]: 0 : return latency != -1 ? latency : ret;
126 : : }
|