| /* |
| * Copyright (c) 2017 FH Dortmund and others |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Description: |
| * CPU Utilization Logger Task for Rover / Raspberry Pi |
| * |
| * Authors: |
| * M. Ozcelikors, R.Hottger |
| * <mozcelikors@gmail.com> <robert.hoettger@fh-dortmund.de> |
| * |
| */ |
| |
| #include "cpu_logger_task.h" |
| #include <string.h> |
| #include <wiringPi.h> |
| #include <ctime> |
| #include <unistd.h> |
| #include "../timing/timing.h" |
| #include "../api/basic_psys_rover.h" |
| #include "../interfaces.h" |
| #include <pthread.h> |
| #include "../RaspberryTest.h" |
| #include <softPwm.h> |
| |
| #include "../pthread_monitoring/collect_thread_name.h" |
| |
| void *Cpu_Logger_Task(void * arg) |
| { |
| FILE* fileptr; |
| char buf[20]; |
| int core_num = 0; |
| double roverUtilCpu[4]; |
| |
| timing cpu_logger_task_tmr; |
| cpu_logger_task_tmr.setTaskID("CpuTsk"); |
| cpu_logger_task_tmr.setDeadline(1); |
| cpu_logger_task_tmr.setPeriod(1); |
| |
| CollectThreadName("Cpu_Logger_Task"); |
| |
| |
| while (1) |
| { |
| cpu_logger_task_tmr.recordStartTime(); |
| cpu_logger_task_tmr.calculatePreviousSlackTime(); |
| |
| //Task content starts here ----------------------------------------------- |
| |
| core_num = 0; |
| |
| // Read from file |
| fileptr = fopen ("/var/www/html/core_usage_rpi.inc", "r"); |
| if (!fileptr) |
| { |
| fprintf(stderr, "Unable to read from file"); |
| abort(); //Dump the core |
| } |
| else |
| { |
| // Get the data |
| fgets(buf, 20, fileptr); |
| #ifdef DEBUG_CPU_LOGGER |
| printf("buf=%s\n", buf); |
| #endif |
| // Parse the read file to get core usage information, Splitting operation with tokens |
| char *token = strtok(buf, ","); |
| |
| while (token != NULL && core_num<=4) |
| { |
| #ifdef DEBUG_CPU_LOGGER |
| printf("token=%s\n", token); |
| #endif |
| sscanf(token, "%lf", &roverUtilCpu[core_num]); |
| token = strtok(NULL, ","); |
| core_num++; |
| } |
| |
| #ifdef DEBUG_CPU_LOGGER |
| printf("CPU_Util=%f %f %f %f\n",roverUtilCpu[0],roverUtilCpu[1],roverUtilCpu[2],roverUtilCpu[3]); |
| #endif |
| // Write it to a shared variable for further usage in the application |
| pthread_mutex_lock(&cpu_util_shared_lock); |
| for (int i = 0; i < 4; i++) |
| cpu_util_shared[i] = roverUtilCpu[i]; |
| pthread_mutex_unlock(&cpu_util_shared_lock); |
| |
| } |
| |
| |
| //Task content ends here ------------------------------------------------- |
| |
| cpu_logger_task_tmr.recordEndTime(); |
| cpu_logger_task_tmr.calculateExecutionTime(); |
| cpu_logger_task_tmr.calculateDeadlineMissPercentage(); |
| cpu_logger_task_tmr.incrementTotalCycles(); |
| pthread_mutex_lock(&cpu_logger_task_ti_l); |
| cpu_logger_task_ti.deadline = cpu_logger_task_tmr.getDeadline(); |
| cpu_logger_task_ti.deadline_miss_percentage = cpu_logger_task_tmr.getDeadlineMissPercentage(); |
| cpu_logger_task_ti.execution_time = cpu_logger_task_tmr.getExecutionTime(); |
| cpu_logger_task_ti.period = cpu_logger_task_tmr.getPeriod(); |
| cpu_logger_task_ti.prev_slack_time = cpu_logger_task_tmr.getPrevSlackTime(); |
| cpu_logger_task_ti.task_id = cpu_logger_task_tmr.getTaskID(); |
| cpu_logger_task_ti.start_time = cpu_logger_task_tmr.getStartTime(); |
| cpu_logger_task_ti.end_time = cpu_logger_task_tmr.getEndTime(); |
| pthread_mutex_unlock(&cpu_logger_task_ti_l); |
| cpu_logger_task_tmr.sleepToMatchPeriod(); |
| } |
| |
| /* the function must return something - NULL will do */ |
| return NULL; |
| } |
| |
| |