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 : RTE_POWER_VALID_LCOREID_OR_ERR_RET(lcore_id, -EINVAL);
31 : 0 : ret = power_get_lcore_mapped_cpu_id(lcore_id, &cpu_id);
32 [ # # ]: 0 : if (ret != 0)
33 : : return ret;
34 : :
35 [ # # ]: 0 : if (latency < 0) {
36 : 0 : POWER_LOG(ERR, "latency should be greater than and equal to 0");
37 : 0 : return -EINVAL;
38 : : }
39 : :
40 : 0 : ret = open_core_sysfs_file(&f, "w", PM_QOS_SYSFILE_RESUME_LATENCY_US, cpu_id);
41 [ # # ]: 0 : if (ret != 0) {
42 : 0 : POWER_LOG(ERR, "Failed to open "PM_QOS_SYSFILE_RESUME_LATENCY_US" : %s",
43 : : cpu_id, strerror(errno));
44 : 0 : return ret;
45 : : }
46 : :
47 : : /*
48 : : * Based on the sysfs interface pm_qos_resume_latency_us under
49 : : * @PM_QOS_SYSFILE_RESUME_LATENCY_US directory in kernel, their meaning
50 : : * is as follows for different input string.
51 : : * 1> the resume latency is 0 if the input is "n/a".
52 : : * 2> the resume latency is no constraint if the input is "0".
53 : : * 3> the resume latency is the actual value to be set.
54 : : */
55 [ # # ]: 0 : if (latency == RTE_POWER_QOS_STRICT_LATENCY_VALUE)
56 : : snprintf(buf, sizeof(buf), "%s", "n/a");
57 [ # # ]: 0 : else if (latency == RTE_POWER_QOS_RESUME_LATENCY_NO_CONSTRAINT)
58 : : snprintf(buf, sizeof(buf), "%u", 0);
59 : : else
60 : : snprintf(buf, sizeof(buf), "%u", latency);
61 : :
62 : 0 : ret = write_core_sysfs_s(f, buf);
63 [ # # ]: 0 : if (ret != 0)
64 : 0 : POWER_LOG(ERR, "Failed to write "PM_QOS_SYSFILE_RESUME_LATENCY_US" : %s",
65 : : cpu_id, strerror(errno));
66 : :
67 : 0 : fclose(f);
68 : :
69 : 0 : return ret;
70 : : }
71 : :
72 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_power_qos_get_cpu_resume_latency, 24.11)
73 : : int
74 : 0 : rte_power_qos_get_cpu_resume_latency(uint16_t lcore_id)
75 : : {
76 : : char buf[PM_QOS_CPU_RESUME_LATENCY_BUF_LEN];
77 : : int latency = -1;
78 : : uint32_t cpu_id;
79 : : FILE *f;
80 : : int ret;
81 : :
82 [ # # # # ]: 0 : RTE_POWER_VALID_LCOREID_OR_ERR_RET(lcore_id, -EINVAL);
83 : 0 : ret = power_get_lcore_mapped_cpu_id(lcore_id, &cpu_id);
84 [ # # ]: 0 : if (ret != 0)
85 : : return ret;
86 : :
87 : 0 : ret = open_core_sysfs_file(&f, "r", PM_QOS_SYSFILE_RESUME_LATENCY_US, cpu_id);
88 [ # # ]: 0 : if (ret != 0) {
89 : 0 : POWER_LOG(ERR, "Failed to open "PM_QOS_SYSFILE_RESUME_LATENCY_US" : %s",
90 : : cpu_id, strerror(errno));
91 : 0 : return ret;
92 : : }
93 : :
94 : 0 : ret = read_core_sysfs_s(f, buf, sizeof(buf));
95 [ # # ]: 0 : if (ret != 0) {
96 : 0 : POWER_LOG(ERR, "Failed to read "PM_QOS_SYSFILE_RESUME_LATENCY_US" : %s",
97 : : cpu_id, strerror(errno));
98 : 0 : goto out;
99 : : }
100 : :
101 : : /*
102 : : * Based on the sysfs interface pm_qos_resume_latency_us under
103 : : * @PM_QOS_SYSFILE_RESUME_LATENCY_US directory in kernel, their meaning
104 : : * is as follows for different output string.
105 : : * 1> the resume latency is 0 if the output is "n/a".
106 : : * 2> the resume latency is no constraint if the output is "0".
107 : : * 3> the resume latency is the actual value in used for other string.
108 : : */
109 [ # # ]: 0 : if (strcmp(buf, "n/a") == 0)
110 : : latency = RTE_POWER_QOS_STRICT_LATENCY_VALUE;
111 : : else {
112 : 0 : latency = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
113 [ # # ]: 0 : latency = latency == 0 ? RTE_POWER_QOS_RESUME_LATENCY_NO_CONSTRAINT : latency;
114 : : }
115 : :
116 : 0 : out:
117 : 0 : fclose(f);
118 : :
119 [ # # ]: 0 : return latency != -1 ? latency : ret;
120 : : }
|