evisum/src/bin/system/machine.c

152 lines
3.5 KiB
C
Raw Normal View History

2018-06-04 03:11:21 -07:00
/*
2020-06-24 05:59:10 -07:00
* Copyright (c) 2018 Alastair Roy Poole <netstar@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2018-06-04 03:11:21 -07:00
*/
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <sys/stat.h>
2018-06-04 03:11:21 -07:00
#include <sys/types.h>
#include <sys/param.h>
#if !defined(__linux__)
2020-04-14 10:24:27 -07:00
# include <sys/sysctl.h>
#endif
2018-06-04 03:11:21 -07:00
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <pthread.h>
#if defined(__APPLE__) && defined(__MACH__)
#define __MacOS__
# include <mach/mach.h>
# include <mach/vm_statistics.h>
# include <mach/mach_types.h>
# include <mach/mach_init.h>
# include <mach/mach_host.h>
# include <net/if_mib.h>
#endif
#if defined(__OpenBSD__)
# include <sys/sched.h>
2018-06-04 03:11:21 -07:00
# include <sys/swap.h>
# include <sys/mount.h>
# include <sys/sensors.h>
# include <net/if_types.h>
# include <ifaddrs.h>
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
# include <net/if_mib.h>
# include <vm/vm_param.h>
# include <dev/acpica/acpiio.h>
2018-06-04 03:11:21 -07:00
#endif
#include "macros.h"
#include "machine.h"
#include "machine/machine.bogox"
#include "machine/cpu.bogox"
#include "machine/memory.bogox"
#include "machine/sensors.bogox"
#include "machine/network.bogox"
2018-06-04 03:11:21 -07:00
2020-06-16 12:12:55 -07:00
static void *
_network_transfer_get_thread_cb(void *arg)
2018-06-04 03:11:21 -07:00
{
network_t *usage = arg;
2018-06-04 03:11:21 -07:00
system_network_transfer_get(usage);
2018-06-04 03:11:21 -07:00
2020-06-16 12:12:55 -07:00
return (void *)0;
2018-06-04 03:11:21 -07:00
}
2018-06-10 06:09:44 -07:00
void
2020-06-17 15:08:44 -07:00
system_info_all_free(Sys_Info *info)
2018-06-04 03:11:21 -07:00
{
2020-06-16 12:12:55 -07:00
sensor_t *snsr;
int i;
2018-06-04 03:11:21 -07:00
2020-06-17 14:34:25 -07:00
for (i = 0; i < info->cpu_count; i++)
2018-06-04 03:11:21 -07:00
{
2020-06-17 14:34:25 -07:00
free(info->cores[i]);
2018-06-04 03:11:21 -07:00
}
2020-06-17 14:34:25 -07:00
free(info->cores);
2018-06-04 03:11:21 -07:00
2020-06-17 14:34:25 -07:00
for (i = 0; i < info->sensor_count; i++)
2020-06-16 12:12:55 -07:00
{
2020-06-17 14:34:25 -07:00
snsr = info->sensors[i];
if (snsr->name)
free(snsr->name);
2020-06-16 12:12:55 -07:00
free(snsr);
}
2020-06-17 14:34:25 -07:00
if (info->sensors)
free(info->sensors);
2018-06-10 06:09:44 -07:00
2020-06-17 14:34:25 -07:00
for (i = 0; i < info->power.battery_count; i++)
2020-06-16 12:12:55 -07:00
{
2020-06-18 04:17:11 -07:00
if (info->power.batteries[i]->name)
free(info->power.batteries[i]->name);
2020-06-18 04:54:45 -07:00
#if defined(__OpenBSD__)
if (info->power.batteries[i]->mibs)
free(info->power.batteries[i]->mibs);
#endif
2020-06-17 14:34:25 -07:00
free(info->power.batteries[i]);
2020-06-16 12:12:55 -07:00
}
2020-06-17 14:34:25 -07:00
if (info->power.batteries)
free(info->power.batteries);
2018-06-10 06:09:44 -07:00
2020-06-17 14:34:25 -07:00
free(info);
2018-06-10 06:09:44 -07:00
}
Sys_Info *
2020-06-17 15:08:44 -07:00
system_info_all_get(void)
2018-06-10 06:09:44 -07:00
{
2020-06-17 14:34:25 -07:00
Sys_Info *info;
2018-06-10 06:09:44 -07:00
void *ret;
pthread_t tid;
int error;
2020-06-17 14:34:25 -07:00
info = calloc(1, sizeof(Sys_Info));
if (!info) return NULL;
2018-06-10 06:09:44 -07:00
info->cores = system_cpu_usage_get(&info->cpu_count);
2018-06-10 06:09:44 -07:00
system_memory_usage_get(&info->memory);
2018-06-10 09:06:34 -07:00
2020-06-24 05:59:10 -07:00
error = pthread_create(&tid, NULL, _network_transfer_get_thread_cb,
&info->network_usage);
2018-06-10 06:09:44 -07:00
if (error)
system_network_transfer_get(&info->network_usage);
2018-06-10 06:09:44 -07:00
system_power_state_get(&info->power);
info->sensors = system_sensors_thermal_get(&info->sensor_count);
2018-06-10 06:09:44 -07:00
if (!error)
{
ret = NULL;
pthread_join(tid, ret);
}
2020-04-14 10:24:27 -07:00
2020-06-17 14:34:25 -07:00
return info;
2018-06-10 06:09:44 -07:00
}