/*
 *  LANTENCY.cpp
 *  
 *
 *  Created by GLevi on 29/09/09.
 *  Dimostrazione del ritardo di latenza nella lettura/scrittura da disco.
 *  Legge da un file e scrive su di un'altro carattere per carattere..... esattamente la cosa da NON FARE.
 *  Poi fa la stesa cosa a grandi blocchi (l'intero file)
 *  Infine usa la chiamata a mmap.
 *  Misura i tempi tramite le liperie OpenMP
 *
 */


#define count 400000000
#include<stdlib.h>
#include<stdio.h>
#include<omp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h> /* mmap() is defined in this header */
#include <fcntl.h>
#include<string.h>
#include <unistd.h>
#include "error.h"

int main(int argc, char *argv[]){
	FILE *inputFilePtr,*outFilePtr; // I Puntatori ai file
	char thechar;               // Buffer di un singolo cararattere 
	double start_time, end_time, ok_time, mmap_time, badrw_time, bad_time; // Variabili per la misura del tempo
	char *File_Buffer;          // Buffer per contenere tutto il file
	int inchar,outchar;	        // Caratteri letti e scritti
	int fdin, fdout;
	void *src, *dst;
	struct stat statbuf;        // Struttura che contiene le informazioni riguardo ai files
	
	if (argc != 3)
		err_quit ("usage: LATENCY <fromfile> <tofile>");
	
	inputFilePtr = fopen(argv[1], "rb");	 // Apre il file in lettura (binaria)
	outFilePtr = fopen(argv[2],"wb"); // Apre il file in scrittura
	
	// Leggo e scrivo carattere per carattere. ********************************************************
	start_time=omp_get_wtime(); // fa partire il timer
	thechar=fgetc(inputFilePtr); //Leggo il primo carattere ........
	while (!feof(inputFilePtr)) // Finchè non trovi un EOF
	{ 
		fputc(thechar,outFilePtr); // Scrive il carattere che ha letto
		thechar=fgetc(inputFilePtr); // legge il successivo carattere
	}
	end_time=omp_get_wtime(); // Fermo il timer
	
	bad_time=end_time-start_time; // calcolo il tempo
	
	
	
	
	// Leggo e scrivo a grandi blocchi.	***************************************************************
	rewind(inputFilePtr);  // Riposiziona il puntatore all'inizio del file di lettura
	fclose(outFilePtr);    // Chiude il file in scrittura
	
	
	File_Buffer= (char *)malloc(count); // Un grosso buffer 
	
	outFilePtr = fopen(argv[2],"wb");  // Riapre il file in scrittura
	
	start_time=omp_get_wtime(); // fa partire il timer
	
	inchar=fread(File_Buffer,sizeof(char),count,inputFilePtr); // Leggo al massimo per la lunghezza del buffer
	
	outchar=fwrite(File_Buffer,sizeof(char),inchar,outFilePtr); // Scrivo solo quelli effettivamente letti
	end_time=omp_get_wtime(); // Stop Timer
	ok_time=end_time-start_time; // Calcola il tempo
	
	fclose(inputFilePtr);    // Chiude il file in lettura
	fclose(outFilePtr);    // Chiude il file in scrittura
	free(File_Buffer);
	 
	
	//printf("La Stringa %s \n",strcat("rm -f ",argv[2]));
	
	start_time=omp_get_wtime(); // fa partire il timer
	
	

	
	
	/* open the input file */
	if ((fdin = open (argv[1], O_RDONLY)) < 0)
		err_sys("can't open %s for reading", argv[1]);
	
	/* open/create the output file */
	if ((fdout = open (argv[2], O_RDWR | O_CREAT | O_TRUNC)) < 0)
		err_sys ("can't create %s for writing", argv[2]);
	
	/* find size of input file */
	if (fstat (fdin,&statbuf) < 0)
		err_sys ("fstat error");
	
	/* go to the location corresponding to the last byte */
	if (lseek (fdout, statbuf.st_size - 1, SEEK_SET) == -1)
		err_sys ("lseek error");
	
	/* write a dummy byte at the last location */
	if (write (fdout, "", 1) != 1)
		err_sys ("write error");
	
	/* mmap the input file */
	if ((src = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0))
		== (caddr_t) -1)
		err_sys ("mmap error for input");
	
	/* mmap the output file */
	if ((dst = mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE,
					 MAP_SHARED, fdout, 0)) == (caddr_t) -1)
		err_sys ("mmap error for output");
	
	/* this copies the input file to the output file */
	memcpy (dst, src, statbuf.st_size);
	
	end_time=omp_get_wtime(); // Stop Timer
	mmap_time=end_time-start_time; // Calcola il tempo
	
	
	

	printf("Bytes Letti, Scritti, Totale: %d , %d , %d \n", inchar, outchar, inchar+outchar);
	printf("1) Tempo  leggendo con fgetc e scrivendo con fputc  (wb) : %f    Bytes per sec : %e \n",bad_time, (inchar+outchar)/bad_time  ); // Stampa
	
	printf("2) Tempo  leggendo con fread e scrivendo con fwrite (wb) : %f    Bytes per sec : %e \n",ok_time,(inchar+outchar)/ok_time);
	
	printf("3) Tempo  leggendo con mmap e scrivendo con  mmap  (wb) : %f    Bytes per sec : %e \n",mmap_time,(inchar+outchar)/mmap_time);
	
	printf("Rapporto (1) / (2) : %f  \n",bad_time/ok_time);
	
	printf("Rapporto (1) / (3) : %f  \n",bad_time/mmap_time);
	printf("Rapporto (2) / (3) : %f  \n",ok_time/mmap_time);
	
}

