procfs: Implement reading load averages 83/180283/6
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Mon, 14 May 2018 06:51:06 +0000 (08:51 +0200)
committerLukasz Stanislawski <l.stanislaws@samsung.com>
Tue, 12 Jun 2018 08:55:25 +0000 (08:55 +0000)
Change-Id: I138e545c1adca80ad34bffd41dcde78e077503c8

src/CMakeLists.txt
src/procfs.c

index ecbf1cf..738674f 100644 (file)
@@ -13,6 +13,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 
 SET(SRCS
        task-worker.c
+       procfs.c
 )
 ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
 
index 3a285e4..4c906db 100644 (file)
  * limitations under the License.
  */
 
+#include <stdio.h>
+
 #include "procfs.h"
+#include "log.h"
+
+#define LOADAVG_FILEPATH "/proc/loadavg"
 
-int procfs_read_system_load_average(struct procfs_load_average_info *avg)
+int procfs_read_system_load_average(struct procfs_load_average_info *info)
 {
-       return -1;
+       float a1, a5, a15;
+
+       FILE *loadavg_fp = fopen(LOADAVG_FILEPATH, "r");
+       if (!loadavg_fp) {
+               ERR("failed to open " LOADAVG_FILEPATH);
+               return -1;
+       }
+
+       if (fscanf(loadavg_fp, "%f %f %f", &a1, &a5, &a15) != 3) {
+               ERR("failed to read " LOADAVG_FILEPATH);
+               fclose(loadavg_fp);
+               return -1;
+       }
+
+       info->one_min_avg = a1;
+       info->five_min_avg = a5;
+       info->fifteen_min_avg = a15;
+
+       fclose(loadavg_fp);
+
+       return 0;
 }
 
 int profcs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage)