MASA-Core
BufferLogger.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "BufferLogger.hpp"
00023 
00024 #include <stdlib.h>
00025 #include "../Timer.hpp"
00026 #include "../../libmasa/libmasa.hpp"
00027 
00028 BufferLogger::BufferLogger(string file)
00029 {
00030         prev_stats.time = 0;
00031         prev_stats.bufferUsage = 0;
00032         prev_stats.blockingTime = 0;
00033         prev_stats.totalBytes = 0;
00034 
00035         this->file = fopen(file.c_str(), "wt");
00036         if (this->file == NULL) {
00037                 fprintf(stderr, "Could not create buffer statistics file.\n");
00038                 exit(1);
00039         }
00040 }
00041 
00042 BufferLogger::~BufferLogger()
00043 {
00044         if (this->file != NULL) {
00045                 fclose(this->file);
00046         }
00047 }
00048 
00049 void BufferLogger::logHeader(int bufferMax) {
00050         fprintf(file, "#buffer_max=%d\n", bufferMax/sizeof(cell_t));
00051         fflush(file);
00052 }
00053 
00054 void BufferLogger::logBuffer(buffer_statistics_t &curr_stats) {
00055         buffer_statistics_t delta_stats = curr_stats - prev_stats;
00056         prev_stats = curr_stats;
00057 
00058         float delta = 0;
00059         float psi = 0;
00060 
00061         int size = sizeof(cell_t);
00062         if (delta_stats.time >= 0.001) {
00063                 delta = ((float)delta_stats.bufferUsage/size)/delta_stats.time;
00064         }
00065         if (delta_stats.time - delta_stats.blockingTime > 0.000001 || delta_stats.time - delta_stats.blockingTime < -0.000001) {
00066                 psi = ((float)delta_stats.totalBytes/size)/delta_stats.time*delta_stats.blockingTime/(delta_stats.time-delta_stats.blockingTime);
00067         }
00068 
00069         fprintf(file, "%.2f\t%d\t%d\t%.2f\t%.2f\t%d\t%d\t%.2f\t%.3f\t%.3f\t%.3f\t%.3f\n",
00070                         curr_stats.time,  // 1
00071                         curr_stats.bufferUsage/size, curr_stats.totalBytes/size, curr_stats.blockingTime,    // 2 3 4
00072                         delta_stats.time, // 5
00073                         delta_stats.bufferUsage/size, delta_stats.totalBytes/size, delta_stats.blockingTime, // 6 7 8
00074                         delta,  psi, // 9 10
00075                         (delta_stats.totalBytes/size)/delta_stats.time, delta+psi); // 11 12
00076 
00077         fflush(file);
00078 }