Skip to content

Commit

Permalink
Added actual proc test and fixed daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
CurlyMoo committed Jul 27, 2017
1 parent 9af0fa0 commit 7cb9017
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 12 deletions.
18 changes: 6 additions & 12 deletions daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ typedef struct clients_t {
int forward;
char media[8];
double cpu;
double ram;
struct clients_t *next;
} clients_t;

Expand Down Expand Up @@ -1197,7 +1196,6 @@ static void *socket_parse_data(int reason, void *param) {
client->forward = 0;
client->stats = 0;
client->cpu = 0;
client->ram = 0;
strcpy(client->media, "all");
client->next = NULL;
client->id = sd;
Expand Down Expand Up @@ -1300,7 +1298,6 @@ static void *socket_parse_data(int reason, void *param) {
tmp_clients = tmp_clients->next;
}
if(exists == 1) {
json_find_number(jvalues, "ram", &client->ram);
json_find_number(jvalues, "cpu", &client->cpu);
}
}
Expand Down Expand Up @@ -1473,13 +1470,10 @@ static void pilight_stats(uv_timer_t *timer_req) {
}

if(stats == 1) {
double cpu = 0.0, ram = 0.0;
double cpu = 0.0;
cpu = getCPUUsage();
ram = getRAMUsage();
if(watchdog == 1 && (cpu > 90)) {
logprintf(LOG_CRIT, "cpu usage too high %f%%, will abort when this persists", cpu);
} else if(watchdog == 1 && (ram > 90)) {
logprintf(LOG_WARNING, "ram usage too high %f%%, will abort when this persists", ram);
} else {
if(watchdog == 1 && stats == 1 && timer_abort_req != NULL) {
uv_timer_stop(timer_abort_req);
Expand All @@ -1497,13 +1491,13 @@ static void pilight_stats(uv_timer_t *timer_req) {
* The missing outer brackets will be
* added in the broadcast function.
*/
snprintf(data->message, 1024, "{\"values\":{\"cpu\":%f,\"ram\":%f},\"origin\":\"core\",\"type\":%d}", cpu, ram, PROCESS);
logprintf(LOG_DEBUG, "cpu: %f%%, ram: %f%%", cpu, ram);
snprintf(data->message, 1024, "{\"values\":{\"cpu\":%f},\"origin\":\"core\",\"type\":%d}", cpu, PROCESS);
logprintf(LOG_DEBUG, "cpu: %f%%", cpu);
struct clients_t *tmp_clients = clients;
while(tmp_clients) {
if(tmp_clients->cpu > 0 && tmp_clients->ram > 0) {
logprintf(LOG_DEBUG, "- client: %s cpu: %f%%, ram: %f%%",
tmp_clients->uuid, tmp_clients->cpu, tmp_clients->ram);
if(tmp_clients->cpu > 0) {
logprintf(LOG_DEBUG, "- client: %s cpu: %f%%",
tmp_clients->uuid, tmp_clients->cpu);
}
tmp_clients = tmp_clients->next;
}
Expand Down
124 changes: 124 additions & 0 deletions tests/proc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright (C) 2013 - 2016 CurlyMo
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <unistd.h>

#include "../libs/pilight/core/CuTest.h"
#include "../libs/pilight/core/pilight.h"
#include "../libs/pilight/core/proc.h"

#include "alltests.h"

pthread_t pth[2];
double cpu[12];
int done[2];

static void do_primes(int maxprimes, int sleep) {
unsigned long i, num, primes = 0;
for(num = 1; num <= maxprimes; ++num) {
for(i = 2; (i <= num) && (num % i != 0); ++i);
if(i == num) {
++primes;
}
if(sleep == 1) {
usleep(1);
}
}
}

static void *thread1(void *param) {
struct cpu_usage_t cpu_usage;
memset(&cpu_usage, 0, sizeof(struct cpu_usage_t));
getThreadCPUUsage(pthread_self(), &cpu_usage);
cpu[0] = cpu_usage.cpu_per;
do_primes(35000, 1);
getThreadCPUUsage(pthread_self(), &cpu_usage);
cpu[1] = cpu_usage.cpu_per;
done[0] = 1;
return NULL;
}

static void *thread2(void *param) {
struct cpu_usage_t cpu_usage;
memset(&cpu_usage, 0, sizeof(struct cpu_usage_t));
getThreadCPUUsage(pthread_self(), &cpu_usage);
cpu[2] = cpu_usage.cpu_per;
do_primes(25000, 1);
getThreadCPUUsage(pthread_self(), &cpu_usage);
cpu[3] = cpu_usage.cpu_per;
done[1] = 1;
return NULL;
}

static void test_proc(CuTest *tc) {
printf("[ %-48s ]\n", __FUNCTION__);
fflush(stdout);

memtrack();

memset(&cpu, 0, 12 * sizeof(double));
memset(&done, 0, 2 * sizeof(int));

{
printf("[ - %-46s ]\n", "calculating 50000 primes");
fflush(stdout);

struct cpu_usage_t cpu_usage;
getThreadCPUUsage(pthread_self(), &cpu_usage);
cpu[0] = cpu_usage.cpu_per;
cpu[1] = getCPUUsage();
do_primes(50000, 0);
cpu[2] = getCPUUsage();
getThreadCPUUsage(pthread_self(), &cpu_usage);
cpu[3] = cpu_usage.cpu_per;
CuAssertTrue(tc, cpu[0] < cpu[3]);
CuAssertTrue(tc, cpu[1] < cpu[2]);
}

{
printf("[ - %-46s ]\n", "now 35000 and 25000 primes in two threads");
fflush(stdout);

struct cpu_usage_t cpu_usage;
cpu[4] = cpu_usage.cpu_per;
memset(&cpu, 0, 12 * sizeof(double));
pthread_create(&pth[0], NULL, thread1, NULL);
pthread_create(&pth[1], NULL, thread2, NULL);
while(done[0] == 0 || done[1] == 0) {
usleep(1000);
}
pthread_join(pth[0], NULL);
pthread_join(pth[1], NULL);
cpu[5] = cpu_usage.cpu_per;

CuAssertTrue(tc,
(cpu[0] < cpu[1]) &&
(cpu[2] < cpu[3]) &&
(cpu[4] < cpu[5]) &&
(cpu[3] < cpu[1]) &&
(cpu[1] < cpu[5]) &&
(cpu[3] < cpu[5]) &&
(cpu[3] < cpu[1])
);
}

CuAssertIntEquals(tc, 0, xfree());
}

CuSuite *suite_proc(void) {
CuSuite *suite = CuSuiteNew();

SUITE_ADD_TEST(suite, test_proc);

return suite;
}

0 comments on commit 7cb9017

Please sign in to comment.