Userspace Resource Manager
Loading...
Searching...
No Matches
CoreAPIsExamples.cpp
1// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2// SPDX-License-Identifier: BSD-3-Clause
3
13#include <iostream>
14
15#include <Urm/UrmAPIs.h>
16
17#define UCLAMP_MIN_RES 0x00030000
18#define UCLAMP_BOOST_VAL 750
19#define TUNE_DURATION 5000 // duration in msec
20#define FAIL -1
21
22// EXAMPLE #1
23// In the following Example the Client Sends:
24// - A Resource Provisioning (Tune) Request to Tune 1 Resource
25// i.e. We try to Provision a Single Resource as part of this Request,
26// The Resource Config for this Resource is as follows (note, Configs are specified via YAML files):
27
28/*
29 * ResourceConfigs:
30 * - ResType: "0x03"
31 * ResID: "0x0000"
32 * Name: "RES_SCHED_UTIL_CLAMP_MIN"
33 * Path: "/proc/sys/kernel/sched_util_clamp_min"
34 * Supported: true
35 * HighThreshold: 1024
36 * LowThreshold: 0
37 * Permissions: "third_party"
38 * Modes: ["display_on", "doze"]
39 * Policy: "lower_is_better"
40 * ApplyType: "global"
41 */
42void func1() {
43 // First Create a List of Resources to be Provisioned as part of this Request
44 // Note multiple Resources can be part of a single Resource Tuner Request
45
46 // Specify the duration as a 64-bit Signed Integer. Note, this value specifies the
47 // Time Interval in milliseconds.
48 int64_t duration = 5000; // Equivalent to 5 seconds
49
50 // Create a 32 bit integer which specifies the Request Properties
51 // This field actually encodes two values:
52 // - Priority (8 bits: 25 - 32)
53 // - Background Processing Status (8 bits: 17 - 24):
54 // Should the Request be Processed in Background (i.e. when the display is in
55 // Off or Doze Mode).
56 int32_t properties = 0;
57
58 // Default Value of 0, corresponds to a Priority of High
59 // and Background Processing Status of False.
60
61 // Create Helpers for these
62 // To set the Priority as Low
63 properties = SET_REQUEST_PRIORITY(properties, REQ_PRIORITY_LOW);
64
65 SysResource resourceList[] = {
66 {
67 .mResCode = UCLAMP_MIN_RES,
68 .mResInfo = 0,
69 .mOptionalInfo = 0,
70 .mNumValues = 1,
71 .mResValue = {
72 .value = UCLAMP_BOOST_VAL,
73 }
74 }
75 };
76
77 // Refer Example#2 for details on opcode construction
78
79 int64_t handle = tuneResources(duration, properties, 1, resourceList);
80
81 // Check the Returned Handle
82 if(handle == -1) {
83 std::cout<<"Request Could not be Sent to the Resource Tuner Server"<<std::endl;
84 } else {
85 std::cout<<"Handle Returned is: "<<handle<<std::endl;
86 }
87
88 // This handle Value can be used for Future Untune / Retune Requests.
89 // Note the Memory allocations made for Resource List will be freed
90 // automatically by the Client Library, and should not be done by the Client itself.
91}
92
93
94// EXAMPLE #2
95// In the following Example the Client Sends:
96// - A Resource Provisioning (Tune) Request to tune a resource, for an infinite duration.
97// - Later the Client Sends an Untune Request to withdraw the previously issued Tune
98// Request, i.e. the Resource will be restored to its original Value.
99void func2() {
100 // Like func1, we first setup the API params
101 // To Provision the Resources for an infinite duration, specify the duration
102 // param in the tuneResources API as -1.
103 int64_t duration = -1;
104
105 // Setup Request Properties
106 int32_t properties = 0;
107
108 // Set the Priority as High
109 properties = SET_REQUEST_PRIORITY(properties, REQ_PRIORITY_HIGH);
110
111 // To mark the Request as eligible for Background Processing
112 // Note, specifying MODE_RESUME is optional, since it is enabled by default.
113 properties = ADD_ALLOWED_MODE(properties, MODE_RESUME);
114 properties = ADD_ALLOWED_MODE(properties, MODE_SUSPEND);
115
116 // Create the List of Resources which need to be Provisioned
117 // Resource Struct Creation
118 SysResource* resourceList = new SysResource[1];
119
120 // Initialize Resource struct Fields:
121
122 // Field: mResCode:
123 // If the opcode is known directly,
124 // resourceList[0].mResCode = 0x00030000;
125
126 // Else if the resType and resId are known then use the CONSTRUCT_RES_CODE macro as follows:
127 resourceList[0].mResCode = CONSTRUCT_RES_CODE(0x03, 0x0000);
128
129 /*
130 * Additional points to Note:
131 * If the resource is user-defined / custom, then annotate the opcode as such:
132 * resourceList[0].mResCode = CUSTOM(resourceList[0].mResCode);
133 * Or in one go:
134 * resourceList[0].mResCode = CUSTOM(CONSTRUCT_RES_CODE(0x03, 0x0000));
135 */
136
137 // Field: mResInfo
138 // Refer func1 for details
139 resourceList[0].mResInfo = 0;
140 // Note, above line of Code is not necessary, since the field is already initialized
141 // to 0 via the Constructor.
142
143 // Field: mOptionalInfo
144 // Refer func1 for details
145 resourceList[0].mOptionalInfo = 0;
146 // Note, above line of Code is not necessary, since the field is already initialized
147 // to 0 via the Constructor.
148
149 // Field: mNumValues
150 // Number of Values to be Configured for this Resource
151 // Resource Tuner supports both Single and Multi Valued Resources
152 // Here we consider the example for a single Valued Resource:
153 resourceList[0].mNumValues = 1;
154
155 // Field: mResValue
156 // Refer func1 for details
157 resourceList[0].mResValue.value = 884;
158
159 // Now our Resource struct is fully constructed
160
161 // Finally we can issue the Resource Provisioning (or Tune) Request
162 int64_t handle = tuneResources(duration, properties, 1, resourceList);
163
164 // Check the Returned Handle
165 if(handle == -1) {
166 std::cout<<"Tune Request could not be sent to the Resource Tuner Server"<<std::endl;
167 return;
168
169 } else {
170 std::cout<<"Handle Returned is: "<<handle<<std::endl;
171 }
172
173 // After some time, say the Client wishes to withdraw the previously issued
174 // Resource Provisioning Request, i.e. restore the Resources to their original Value.
175
176 // Issue an Untune Request
177 int8_t status = untuneResources(handle);
178
179 if(status == -1) {
180 std::cout<<"Untune Request could not be sent to the Resource Tuner Server"<<std::endl;
181 }
182}
183
184
185// EXAMPLE #3
186// In the following Example the Client Sends:
187// - A Resource Provisioning (Tune) Request to tune a resource, for some duration.
188// - Later the Client wishes to Modify the duration of this Request, to do so
189// a Retune Request is issued to the Resource Tuner Server.
190void func3() {
191 // Provision for 8 seconds (8000 milliseconds)
192 int64_t duration = 8000;
193
194 // Setup Request Properties
195 int32_t properties = 0;
196
197 // Here the Priority is High
198 properties = SET_REQUEST_PRIORITY(properties, REQ_PRIORITY_HIGH);
199
200 // This Request should only be processed when the Device Display is On,
201 // i.e. not a background Request.
202 // Optional (since MODE_RESUME is enabled by default)
203 properties = ADD_ALLOWED_MODE(properties, MODE_RESUME);
204
205 // Create the List of Resources which need to be Provisioned
206 // Resource Struct Creation
207 SysResource* resourceList = new SysResource[1];
208
209 // Initialize Resource struct Fields:
210
211 // Field: mResCode:
212 // Refer func1 for details
213 resourceList[0].mResCode = 0x00030000;
214
215 // Field: mResInfo
216 // Refer func1 for details
217 resourceList[0].mResInfo = 0;
218 // Note, above line of Code is not necessary, since the field is already initialized
219 // to 0 via the Constructor.
220
221 // Field: mOptionalInfo
222 // Refer func1 for details
223 resourceList[0].mOptionalInfo = 0;
224 // Note, above line of Code is not necessary, since the field is already initialized
225 // to 0 via the Constructor.
226
227 // Field: mNumValues
228 // Number of Values to be Configured for this Resource
229 // Resource Tuner supports both Single and Multi Valued Resources
230 // Here we consider the example for a single Valued Resource:
231 resourceList[0].mNumValues = 1;
232
233 // Field: mResValue
234 // Refer func1 for details
235 resourceList[0].mResValue.value = 884;
236
237 // Now our Resource struct is fully constructed
238
239 // Finally we can issue the Resource Provisioning (or Tune) Request
240 int64_t handle = tuneResources(duration, properties, 1, resourceList);
241
242 // Check the Returned Handle
243 if(handle == -1) {
244 std::cout<<"Tune Request could not be sent to the Resource Tuner Server"<<std::endl;
245 return;
246
247 } else {
248 std::cout<<"Handle Returned is: "<<handle<<std::endl;
249 }
250
251 // After some time, say the Client wishes to extend the duration of the previously
252 // issued Resource Provisioning Request, they can do so by using the retuneResources API.
253
254 // Issue a Retune Request
255 int64_t newDuration = 20000;
256 int8_t status = retuneResources(handle, newDuration);
257
258 if(status == -1) {
259 std::cout<<"Untune Request could not be sent to the Resource Tuner Server"<<std::endl;
260 }
261}
262
263int32_t main(int32_t argc, char* argv[]) {
264 func1();
265}
266
267// Compilation Notes:
268// The executable needs to be linked to the RestuneClient lib, where these APIs
269// are defined. This can be done, as follows:
270// GCC: g++ CoreAPIsExamples.cpp -o ResourceTunerCoreAPIs -lClientAPIs
271// CMake: This can be done as part of the C/C++ project by linking to the Library
272// via the target_link_libraries command. For example, if the executalbe is called clientExec,
273// it can be linked as follows:
274// target_link_libraries(clientExec RestuneClient)
275
276// More Examples?
277// Refer: IntegrationTests.cpp in the $(root)/Tests/Integration/ directory, which exhaustively
278// demonstrates the use of all these APIs. This file contains test code which mirrors the client perspective.
int64_t tuneResources(int64_t duration, int32_t prop, int32_t numRes, SysResource *resourceList)
Tune Resource Values for finite or finite duration.
int8_t retuneResources(int64_t handle, int64_t duration)
Modify the duration of a previously issued Tune Request.
int8_t untuneResources(int64_t handle)
Release (or free) the Request with the given handle.
Used to store information regarding Resources / Tunables which need to be Provisioned as part of the ...
int32_t value
Use this field for single Valued Resources.
uint32_t mResCode
A uniqued 32-bit (unsigned) identifier for the Resource.
int32_t mNumValues
Number of values to be configured for the Resource, both single-valued and multi-valued Resources are...
int32_t mResInfo
Holds Logical Core and Cluster Information:
union SysResource::@123143110140375143216027027064266051075023012302 mResValue
The value to be Configured for this Resource Node.
int32_t mOptionalInfo
Field to hold optional information for Request Processing.