#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define INFO_PATH "/opt/huawei/ng_cdn/rofs/ne/60817410/chassis/60817413/board/1/logical/os/1/mem/info"
int distribute(int total, int free, int percent)
{
int i_used = 0;
int i_usemore = 0;
int i_percent = percent;
char *a = NULL;
int i = 0;
if(NULL== &total || NULL== &free)
{
perror("distribute");
exit(0);
}
i_used = total - free;
i_usemore = (i_percent -1) * (total) / 100 - (i_used);
for (i=0; i<i_usemore; i++)
{
a = (char *)malloc(1024);
memset(a,0,1024);
}
return 0;
}
char* deal_info(char* sline, int c)
{
int i = 0;
int k = 0;
int tmp = c;
char *str = NULL;
if (NULL == sline)
{
perror("deal_info");
exit(0);
}
str = sline;
for(i=0; i<tmp; i++)
{
if(i == tmp -1)
{
k = 0;
char* info = (char *)malloc(40);
memset(info,0,40);
while(*str++ != '\n')
{
if(*str <='9' && *str >='0')
{
info[k++] = *str;
}
}
return info;
}
while(*str++ != '\n')
{
NULL;
}
}
return NULL;
}
int main(int argc, char **argv)
{
int i = 0;
int i_total = 0;
int i_free = 0;
int i_tmptotal = 0;
int i_tmpfree = 0;
char *c_total = NULL;
char *c_free = NULL;
FILE *fp = NULL;
if (argc != 2)
{
printf("\r\ninput error\n");
return -1;
}
int tmp_percent = atoi(argv[1]);
printf("memPercent : %d%\n",tmp_percent);
if(NULL == (fp = fopen(INFO_PATH,"r")))
{
perror("fopen");
exit(0);
}
fclose(fp);
char sLine[1024];
while(!feof(fp))
{
fp = fopen(INFO_PATH,"r");
for(i=0;i<10;i++)
{
fp = fopen(INFO_PATH,"r");
memset(sLine,0,1024);
fread(sLine,25,2,fp);
c_total = deal_info(sLine,1);
c_free = deal_info(sLine,2);
i_tmptotal += atoi(c_total);
i_tmpfree += atoi(c_free);
free(c_total);
free(c_free);
fclose(fp);
}
i_total = i_tmptotal /10;
i_free = i_tmpfree /10;
break;
}
printf("*******************************\n");
printf("memTotal :%d\n", i_total);
printf("memFree :%d\n", i_free);
printf("*******************************\n\n");
distribute(i_total, i_free, tmp_percent);
sleep(1);
fp = fopen(INFO_PATH,"r");
memset(sLine,0,1024);
fread(sLine,30,2,fp);
c_total = deal_info(sLine,1);
c_free = deal_info(sLine,2);
i_tmptotal = atoi(c_total);
i_tmpfree = atoi(c_free);
printf("NOW MEM: ----------------------------\n");
printf("memTotal : %d KB\n",i_tmptotal);
printf("memFree : %d KB\n",i_tmpfree);
printf("memUsed : %d KB\n",i_tmptotal - i_tmpfree);
printf("memPercent : %.2f%\n",(float)(i_tmptotal - i_tmpfree)*100 /(float)(i_tmptotal));
printf("--------------------------------------\n");
fclose(fp);
printf("OK!Distribute Success!\nNOW SLEEP TILL YOU KILL ME!!!!!!!!!!!\n");
sleep (2000);
return 0;
}