1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/types.h>
13 #include <sys/epoll.h>
14 #include <sys/queue.h>
18 #include <rte_memory.h>
19 #include <rte_malloc.h>
20 #include <rte_atomic.h>
21 #include <rte_cycles.h>
22 #include <rte_ethdev.h>
23 #include <rte_pmd_i40e.h>
25 #include <libvirt/libvirt.h>
26 #include "channel_monitor.h"
27 #include "channel_commands.h"
28 #include "channel_manager.h"
29 #include "power_manager.h"
31 #define RTE_LOGTYPE_CHANNEL_MONITOR RTE_LOGTYPE_USER1
33 #define MAX_EVENTS 256
35 uint64_t vsi_pkt_count_prev[384];
36 uint64_t rdtsc_prev[384];
38 double time_period_ms = 1;
39 static volatile unsigned run_loop = 1;
40 static int global_event_fd;
41 static unsigned int policy_is_set;
42 static struct epoll_event *global_events_list;
43 static struct policy policies[MAX_VMS];
45 void channel_monitor_exit(void)
48 rte_free(global_events_list);
52 core_share(int pNo, int z, int x, int t)
54 if (policies[pNo].core_share[z].pcpu == lvm_info[x].pcpus[t]) {
55 if (strcmp(policies[pNo].pkt.vm_name,
56 lvm_info[x].vm_name) != 0) {
57 policies[pNo].core_share[z].status = 1;
58 power_manager_scale_core_max(
59 policies[pNo].core_share[z].pcpu);
65 core_share_status(int pNo)
68 int noVms, noVcpus, z, x, t;
70 get_all_vm(&noVms, &noVcpus);
72 /* Reset Core Share Status. */
73 for (z = 0; z < noVcpus; z++)
74 policies[pNo].core_share[z].status = 0;
76 /* Foreach vcpu in a policy. */
77 for (z = 0; z < policies[pNo].pkt.num_vcpu; z++) {
78 /* Foreach VM on the platform. */
79 for (x = 0; x < noVms; x++) {
80 /* Foreach vcpu of VMs on platform. */
81 for (t = 0; t < lvm_info[x].num_cpus; t++)
82 core_share(pNo, z, x, t);
88 get_pcpu_to_control(struct policy *pol)
91 /* Convert vcpu to pcpu. */
96 RTE_LOG(INFO, CHANNEL_MONITOR, "Looking for pcpu for %s\n",
98 get_info_vm(pol->pkt.vm_name, &info);
100 for (count = 0; count < pol->pkt.num_vcpu; count++) {
101 mask_u64b = info.pcpu_mask[pol->pkt.vcpu_to_control[count]];
102 for (pcpu = 0; mask_u64b; mask_u64b &= ~(1ULL << pcpu++)) {
103 if ((mask_u64b >> pcpu) & 1)
104 pol->core_share[count].pcpu = pcpu;
110 get_pfid(struct policy *pol)
115 for (i = 0; i < pol->pkt.nb_mac_to_monitor; i++) {
117 RTE_ETH_FOREACH_DEV(x) {
118 ret = rte_pmd_i40e_query_vfid_by_mac(x,
119 (struct ether_addr *)&(pol->pkt.vfid[i]));
120 if (ret != -EINVAL) {
125 if (ret == -EINVAL || ret == -ENOTSUP || ret == ENODEV) {
126 RTE_LOG(INFO, CHANNEL_MONITOR,
127 "Error with Policy. MAC not found on "
138 update_policy(struct channel_packet *pkt)
141 unsigned int updated = 0;
144 for (i = 0; i < MAX_VMS; i++) {
145 if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
146 policies[i].pkt = *pkt;
147 get_pcpu_to_control(&policies[i]);
148 if (get_pfid(&policies[i]) == -1) {
152 core_share_status(i);
153 policies[i].enabled = 1;
158 for (i = 0; i < MAX_VMS; i++) {
159 if (policies[i].enabled == 0) {
160 policies[i].pkt = *pkt;
161 get_pcpu_to_control(&policies[i]);
162 if (get_pfid(&policies[i]) == -1)
164 core_share_status(i);
165 policies[i].enabled = 1;
174 get_pkt_diff(struct policy *pol)
177 uint64_t vsi_pkt_count,
179 vsi_pkt_count_prev_total = 0;
180 double rdtsc_curr, rdtsc_diff, diff;
182 struct rte_eth_stats vf_stats;
184 for (x = 0; x < pol->pkt.nb_mac_to_monitor; x++) {
187 if (rte_pmd_i40e_get_vf_stats(x, pol->pfid[x], &vf_stats) == 0)
188 vsi_pkt_count = vf_stats.ipackets;
192 vsi_pkt_total += vsi_pkt_count;
194 vsi_pkt_count_prev_total += vsi_pkt_count_prev[pol->pfid[x]];
195 vsi_pkt_count_prev[pol->pfid[x]] = vsi_pkt_count;
198 rdtsc_curr = rte_rdtsc_precise();
199 rdtsc_diff = rdtsc_curr - rdtsc_prev[pol->pfid[x-1]];
200 rdtsc_prev[pol->pfid[x-1]] = rdtsc_curr;
202 diff = (vsi_pkt_total - vsi_pkt_count_prev_total) *
203 ((double)rte_get_tsc_hz() / rdtsc_diff);
209 apply_traffic_profile(struct policy *pol)
215 diff = get_pkt_diff(pol);
217 RTE_LOG(INFO, CHANNEL_MONITOR, "Applying traffic profile\n");
219 if (diff >= (pol->pkt.traffic_policy.max_max_packet_thresh)) {
220 for (count = 0; count < pol->pkt.num_vcpu; count++) {
221 if (pol->core_share[count].status != 1)
222 power_manager_scale_core_max(
223 pol->core_share[count].pcpu);
225 } else if (diff >= (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
226 for (count = 0; count < pol->pkt.num_vcpu; count++) {
227 if (pol->core_share[count].status != 1)
228 power_manager_scale_core_med(
229 pol->core_share[count].pcpu);
231 } else if (diff < (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
232 for (count = 0; count < pol->pkt.num_vcpu; count++) {
233 if (pol->core_share[count].status != 1)
234 power_manager_scale_core_min(
235 pol->core_share[count].pcpu);
241 apply_time_profile(struct policy *pol)
247 char time_string[40];
249 /* Obtain the time of day, and convert it to a tm struct. */
250 gettimeofday(&tv, NULL);
251 ptm = localtime(&tv.tv_sec);
252 /* Format the date and time, down to a single second. */
253 strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);
255 for (x = 0; x < HOURS; x++) {
257 if (ptm->tm_hour == pol->pkt.timer_policy.busy_hours[x]) {
258 for (count = 0; count < pol->pkt.num_vcpu; count++) {
259 if (pol->core_share[count].status != 1) {
260 power_manager_scale_core_max(
261 pol->core_share[count].pcpu);
262 RTE_LOG(INFO, CHANNEL_MONITOR,
263 "Scaling up core %d to max\n",
264 pol->core_share[count].pcpu);
268 } else if (ptm->tm_hour ==
269 pol->pkt.timer_policy.quiet_hours[x]) {
270 for (count = 0; count < pol->pkt.num_vcpu; count++) {
271 if (pol->core_share[count].status != 1) {
272 power_manager_scale_core_min(
273 pol->core_share[count].pcpu);
274 RTE_LOG(INFO, CHANNEL_MONITOR,
275 "Scaling down core %d to min\n",
276 pol->core_share[count].pcpu);
280 } else if (ptm->tm_hour ==
281 pol->pkt.timer_policy.hours_to_use_traffic_profile[x]) {
282 apply_traffic_profile(pol);
289 apply_workload_profile(struct policy *pol)
294 if (pol->pkt.workload == HIGH) {
295 for (count = 0; count < pol->pkt.num_vcpu; count++) {
296 if (pol->core_share[count].status != 1)
297 power_manager_scale_core_max(
298 pol->core_share[count].pcpu);
300 } else if (pol->pkt.workload == MEDIUM) {
301 for (count = 0; count < pol->pkt.num_vcpu; count++) {
302 if (pol->core_share[count].status != 1)
303 power_manager_scale_core_med(
304 pol->core_share[count].pcpu);
306 } else if (pol->pkt.workload == LOW) {
307 for (count = 0; count < pol->pkt.num_vcpu; count++) {
308 if (pol->core_share[count].status != 1)
309 power_manager_scale_core_min(
310 pol->core_share[count].pcpu);
316 apply_policy(struct policy *pol)
319 struct channel_packet *pkt = &pol->pkt;
321 /*Check policy to use*/
322 if (pkt->policy_to_use == TRAFFIC)
323 apply_traffic_profile(pol);
324 else if (pkt->policy_to_use == TIME)
325 apply_time_profile(pol);
326 else if (pkt->policy_to_use == WORKLOAD)
327 apply_workload_profile(pol);
332 process_request(struct channel_packet *pkt, struct channel_info *chan_info)
336 if (chan_info == NULL)
339 if (rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_CONNECTED,
340 CHANNEL_MGR_CHANNEL_PROCESSING) == 0)
343 if (pkt->command == CPU_POWER) {
344 core_mask = get_pcpus_mask(chan_info, pkt->resource_id);
345 if (core_mask == 0) {
346 RTE_LOG(ERR, CHANNEL_MONITOR, "Error get physical CPU mask for "
347 "channel '%s' using vCPU(%u)\n", chan_info->channel_path,
348 (unsigned)pkt->unit);
351 if (__builtin_popcountll(core_mask) == 1) {
353 unsigned core_num = __builtin_ffsll(core_mask) - 1;
356 case(CPU_POWER_SCALE_MIN):
357 power_manager_scale_core_min(core_num);
359 case(CPU_POWER_SCALE_MAX):
360 power_manager_scale_core_max(core_num);
362 case(CPU_POWER_SCALE_DOWN):
363 power_manager_scale_core_down(core_num);
365 case(CPU_POWER_SCALE_UP):
366 power_manager_scale_core_up(core_num);
368 case(CPU_POWER_ENABLE_TURBO):
369 power_manager_enable_turbo_core(core_num);
371 case(CPU_POWER_DISABLE_TURBO):
372 power_manager_disable_turbo_core(core_num);
379 case(CPU_POWER_SCALE_MIN):
380 power_manager_scale_mask_min(core_mask);
382 case(CPU_POWER_SCALE_MAX):
383 power_manager_scale_mask_max(core_mask);
385 case(CPU_POWER_SCALE_DOWN):
386 power_manager_scale_mask_down(core_mask);
388 case(CPU_POWER_SCALE_UP):
389 power_manager_scale_mask_up(core_mask);
391 case(CPU_POWER_ENABLE_TURBO):
392 power_manager_enable_turbo_mask(core_mask);
394 case(CPU_POWER_DISABLE_TURBO):
395 power_manager_disable_turbo_mask(core_mask);
404 if (pkt->command == PKT_POLICY) {
405 RTE_LOG(INFO, CHANNEL_MONITOR, "\nProcessing Policy request from Guest\n");
410 /* Return is not checked as channel status may have been set to DISABLED
411 * from management thread
413 rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_PROCESSING,
414 CHANNEL_MGR_CHANNEL_CONNECTED);
420 add_channel_to_monitor(struct channel_info **chan_info)
422 struct channel_info *info = *chan_info;
423 struct epoll_event event;
425 event.events = EPOLLIN;
426 event.data.ptr = info;
427 if (epoll_ctl(global_event_fd, EPOLL_CTL_ADD, info->fd, &event) < 0) {
428 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to add channel '%s' "
429 "to epoll\n", info->channel_path);
436 remove_channel_from_monitor(struct channel_info *chan_info)
438 if (epoll_ctl(global_event_fd, EPOLL_CTL_DEL, chan_info->fd, NULL) < 0) {
439 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to remove channel '%s' "
440 "from epoll\n", chan_info->channel_path);
447 channel_monitor_init(void)
449 global_event_fd = epoll_create1(0);
450 if (global_event_fd == 0) {
451 RTE_LOG(ERR, CHANNEL_MONITOR, "Error creating epoll context with "
452 "error %s\n", strerror(errno));
455 global_events_list = rte_malloc("epoll_events", sizeof(*global_events_list)
456 * MAX_EVENTS, RTE_CACHE_LINE_SIZE);
457 if (global_events_list == NULL) {
458 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to rte_malloc for "
466 run_channel_monitor(void)
471 n_events = epoll_wait(global_event_fd, global_events_list,
475 for (i = 0; i < n_events; i++) {
476 struct channel_info *chan_info = (struct channel_info *)
477 global_events_list[i].data.ptr;
478 if ((global_events_list[i].events & EPOLLERR) ||
479 (global_events_list[i].events & EPOLLHUP)) {
480 RTE_LOG(DEBUG, CHANNEL_MONITOR, "Remote closed connection for "
482 chan_info->channel_path);
483 remove_channel(&chan_info);
486 if (global_events_list[i].events & EPOLLIN) {
488 int n_bytes, err = 0;
489 struct channel_packet pkt;
491 int buffer_len = sizeof(pkt);
493 while (buffer_len > 0) {
494 n_bytes = read(chan_info->fd,
496 if (n_bytes == buffer_len)
500 RTE_LOG(DEBUG, CHANNEL_MONITOR,
502 "channel '%s' read: %s\n",
503 chan_info->channel_path,
505 remove_channel(&chan_info);
508 buffer = (char *)buffer + n_bytes;
509 buffer_len -= n_bytes;
512 process_request(&pkt, chan_info);
515 rte_delay_us(time_period_ms*1000);
519 for (j = 0; j < MAX_VMS; j++) {
520 if (policies[j].enabled == 1)
521 apply_policy(&policies[j]);