#include "util.h"

static int net_status_led;//用于获取NET状态

struct sysinfo info;

double cal_occupy(CPU_OCCUPY *c1, CPU_OCCUPY *c2) {
  double t1, t2;
  double id, sd;
  double cpu_used;

  t1 = (double)(c1->user + c1->nice + c1->system + c1->idle);
  t2 = (double)(c2->user + c2->nice + c2->system + c2->idle);

  id = (double)(c2->user - c1->user);
  sd = (double)(c2->system - c1->system);

  cpu_used = (100 * (id + sd) / (t2 - t1));
  return cpu_used;
}

void get_disk_usage(char *path,double *mem_used_persent,unsigned long long *mem_used,unsigned long long *mem_totle)
{
    struct statvfs disk_info;
    // const char *path = "/";  // 要获取信息的挂载点路径

    if (statvfs(path, &disk_info) == 0) {
        unsigned long long block_size = disk_info.f_bsize;       // 块大小
        unsigned long long total_blocks = disk_info.f_blocks;    // 总块数
        // unsigned long long free_blocks = disk_info.f_bfree;      // 空闲块数
        unsigned long long available_blocks = disk_info.f_bavail;// 可用块数
        unsigned long long total_size = block_size * total_blocks;
        // unsigned long long free_size = block_size * free_blocks;
        unsigned long long available_size = block_size * available_blocks;
        unsigned long long used_size = total_size - available_size;
        if (mem_used_persent!=NULL)
            *mem_used_persent = used_size*100.0f /total_size;
        if (mem_used!=NULL)
            *mem_used=used_size;
        if (mem_totle!=NULL)
            *mem_totle=total_size;
    } else {
        perror("Failed to retrieve disk information");
    }
}

void get_cpuoccupy(CPU_OCCUPY *cpu) {

  char buff[512];
  FILE *fd;

  /* /proc/stat 包含了系统启动以来的许多关于kernel和系统的统计信息，
  其中包括CPU运行情况、中断统计、启动时间、上下文切换次数、
  运行中的进程等等信息 */
  fd = fopen("/proc/stat", "r");
  if (fd == NULL) {
    perror("open /proc/stat failed\n");
    exit(0);
  }
  fgets(buff, sizeof(buff), fd);

  sscanf(buff, "%s %u %u %u %u", cpu->name, &cpu->user, &cpu->nice,
         &cpu->system, &cpu->idle);
  fclose(fd);
}

void get_mem_usage(double *mem_used) {

  FILE *fd;
  /************
  ** /proc / meminfo中包含系统允许内存信息
  **
  ****/

  fd = fopen("/proc/meminfo", "r");
  if (fd == NULL) {
    perror("open /proc/meminfo failed\n");
    exit(0);
  }

  size_t bytes_read;
  size_t read;
  char *line = NULL;
  /************
   *mem_used = total-memavailable
   *******/
  int avimem = 0;

  while ((read = getline(&line, &bytes_read, fd)) != -1) {
    if (strstr(line, "MemAvailable") != NULL) {
      sscanf(line, "%*s%d%*s", &avimem);
      break;
    }
  }
  if (line != NULL)
  {
    free(line);
  }
  int t = info.totalram / 1024.0;
  *mem_used = (t - avimem) * 100 / t;

  fclose(fd);
}


void get_cpu_usage(double *cpu_used) {
  CPU_OCCUPY cpu_0, cpu_1;

  get_cpuoccupy(&cpu_0);
  /*   while (1)  {
  //实现循环监控CPU状态，间隔时间1s */
  sleep(1);
  get_cpuoccupy(&cpu_1);

  *cpu_used = cal_occupy(&cpu_0, &cpu_1);
#if DEBUG
  printf("cpu: %.2f%%\n", *cpu_used);
#endif
  /*   cpu_0 = cpu_1;//循环监控打开，结构体拷贝赋值语句
} */
}

void get_net_usage(int *net_status)
{
    *net_status = net_status_led;
}