/*  This file is part of lsnet. 
    Copyright (C) 2009-2010  Sterling Pickens
    Lsnet is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Lsnet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with lsnet.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "meminfo.h"

struct mem_data mem;

void Mem_allocate(){
	unsigned int count_entries = 0;
	mem.poll_all = malloc(sizeof(void *) * 8);
	while(count_entries < 8){
		mem.poll_all[count_entries] = malloc(sizeof(long long) * mem.polls);
		count_entries++;
	}
	printf("long long size: %lu\n", (long)sizeof(long long) );
}

void Mem_ten_min(){
	FILE* fp;
	unsigned int count_poll = 0;
	unsigned int count_minutes = 0;
	unsigned int count_entries = 0;
	
	struct tm *local;
	time_t t;
	local = localtime(&t);
	//char *one_str; //date + space + 10*(memvalues(10) + space) + newline + null
	char *tmp_str;
	unsigned int tmp_str_size = ceil(log( pow(2, sizeof(long long)*8))) + 2;
	unsigned int ten_str_size = 10*(strlen(asctime(local)) + 1 + 10*tmp_str_size + 1) + 1;
	//unsigned int ten_str_size = 10*(strlen(asctime(local)) + 1 + 10*(10+1) + 1 ) + 1;
	char *ten_str; //10x one_str - 9 extra nulls

	//unsigned int one_str_size = strlen(asctime(local)) + 1 + 10*(10+1) + 1 + 1;
	//unsigned int ten_str_size = one_str_size*10 - 9;
	//one_str = malloc( one_str_size );
	ten_str = malloc( ten_str_size ); 
	tmp_str = malloc( tmp_str_size );
	

	while (1 == 1){
		memset ((void*) ten_str, '\0', ten_str_size);
		for(count_minutes=0; count_minutes<10; count_minutes++){
			//memset ((void*) one_str, '\0', one_str_size);
			for(count_entries=0; count_entries<8; count_entries++){
				mem.min_one[count_entries] = 0;
			}
			Mem_one_min();
			t = time(NULL);
			local = localtime(&t);
			strcat(ten_str, asctime(local));
			ten_str[strlen(ten_str)-1]='\0'; //newline gone
			
			for(count_poll=0; count_poll < mem.polls; count_poll++){
				for (count_entries = 0; count_entries < 8; count_entries++){
					//only save the polling interval variables with the highest memused instance
					if(mem.min_one[count_entries] < mem.poll_all[count_entries][count_poll]){
						mem.min_one[count_entries] = mem.poll_all[count_entries][count_poll];
					}
				}
			}
			for (count_entries = 0; count_entries < 8; count_entries++){
				if(sprintf( tmp_str, " %lu", mem.min_one[count_entries]) < 1){
					//FAIL
					printf("Error: sprintf failed!\n");
					exit(1);
				}
				strcat(ten_str, tmp_str);
			}
			strcat(ten_str, "\n");
			//strcat(ten_str, one_str);
		}
		if (fp = fopen(global_strs.mem_log, "a")){
			fprintf(fp, "%s", ten_str);
		}
		fclose(fp);
	}
}

static void Mem_one_min(){
	unsigned int count_poll=0;
	unsigned int count_entries=0;
	for(count_poll=0; count_poll<mem.polls; count_poll++){
		Get_meminfo();
		for(count_entries=0; count_entries<8; count_entries++){
			mem.poll_all[count_entries][count_poll] = mem.poll_one[count_entries];
		}
	}
}

static void Get_meminfo() 
{
	sleep(mem.frequency);

        FILE* fp;
        unsigned int count = 0;
	unsigned int count2;
	unsigned int memused, swapused;
	//my($memsize,$memfree,$buffers,$cached,$memused,$swaptotal,$swapfree,$swapused);
	//VmallocTotal:   34359738367 kB

	char buf[36];
	char temp[2];
	char bufstr[sizeof(temp)*12];
	char memtotal[sizeof(bufstr)+1];
	char memfree[sizeof(bufstr)+1];
	char buffers[sizeof(bufstr)+1];
	char cached[sizeof(bufstr)+1];
	char swaptotal[sizeof(bufstr)+1];
	char swapfree[sizeof(bufstr)+1];

        if( ( fp = fopen( "/proc/meminfo", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /proc/meminfo\n" );
                exit( 1 );
        }
        while( fgets(buf, sizeof(buf), fp) != NULL)
        {
		//MemTotal:      2075412 kB
		//MemFree:        417812 kB
		//Buffers:        730692 kB
		count = 0;

			//MemTotal:       776960 kB
		if (buf[count] == 'M'){
                	if (buf[count+1] == 'e'){
                        	if (buf[count+2] == 'm'){
                                	if (buf[count+3] == 'T'){ //count+13 till end of buf is frequency
                                        	if (buf[count+4] == 'o')
                                                {
								count2 = 9;
								while(buf[count2] == ' ') {
									count2++;
								}
								temp[0]=buf[count2];
								temp[1]=0;
								strcpy(bufstr, temp);
								for( (count2 = count2 + 1); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
									temp[0]=buf[count2];
									temp[1]=0;
									strcat(bufstr,temp);									

								}
								strcpy(memtotal, bufstr);
                                                }
                                        }
                                }
                        }
                }
			//MemFree:         39520 kB
                if (buf[count] == 'M'){
                        if (buf[count+1] == 'e'){
                                if (buf[count+2] == 'm'){
                                        if (buf[count+3] == 'F'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'r')
                                                {
                                                                count2 = 8;
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                }
								strcpy(memfree, bufstr);
                                                }
                                        }
                                }
                        }
                }
			//Buffers:        193200 kB
                if (buf[count] == 'B'){
                        if (buf[count+1] == 'u'){
                                if (buf[count+2] == 'f'){
                                        if (buf[count+3] == 'f'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'e')
                                                {
                                                                count2 = 8;
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								strcpy(buffers, bufstr);
                                                }
                                        }
                                }
                        }
                }
			//Cached:         222660 kB
                if (buf[count] == 'C'){
                        if (buf[count+1] == 'a'){
                                if (buf[count+2] == 'c'){
                                        if (buf[count+3] == 'h'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'e')
                                                {
                                                                count2 = 7;
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								strcpy(cached, bufstr);
                                                }
                                        }
                                }
                        }
                }
			//SwapTotal:      674720 kB
                if (buf[count] == 'S'){
                        if (buf[count+1] == 'w'){
                                if (buf[count+2] == 'a'){
                                        if (buf[count+3] == 'p'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'T')
                                                {
                                                                count2 = 10;
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								strcpy(swaptotal, bufstr);
                                                }
                                        }
                                }
                        }
                }
			//SwapFree:       672600 kB
                if (buf[count] == 'S'){
                        if (buf[count+1] == 'w'){
                                if (buf[count+2] == 'a'){
                                        if (buf[count+3] == 'p'){ //count+13 till end of buf is frequency
                                                if (buf[count+4] == 'F')
                                                {
                                                                count2 = 9;
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								temp[0]=buf[count2];
								temp[1]=0;
                                                                strcpy(bufstr, temp);
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf)&& buf[count2] != ' '); count2++)
								{
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                 
                                                                }
								strcpy(swapfree, bufstr);
                                                }
                                        }
                                }
                        }
                }

	}
	fclose( fp );

	mem.poll_one[0] = atoi(memtotal);
	mem.poll_one[1] = atoi(memfree);
	mem.poll_one[2] = atoi(buffers);
	mem.poll_one[3] = atoi(cached);
	memused = mem.poll_one[0] - mem.poll_one[1];
	mem.poll_one[4] = memused;
	mem.poll_one[5] = atoi(swaptotal);
	mem.poll_one[6] = atoi(swapfree);
	swapused = mem.poll_one[5] - mem.poll_one[6];
	mem.poll_one[7] = swapused;
}

