/* praat_statistics.c
 *
 * Copyright (C) 1992-2002 Paul Boersma
 *
 * This program 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 2 of the License, or (at
 * your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * pb 1997/05/18
 * pb 2002/03/07 GPL
 * pb 2002/03/09 OSX no mention of free memory
 */

#include <time.h>
#include "praatP.h"

static struct {
	long batchSessions, interactiveSessions;
	double memory;
	char dateOfFirstSession [50];
} statistics;

void praat_statistics_prefs (void) {
	Resources_addLong ("PraatShell.batchSessions", & statistics.batchSessions);
	Resources_addLong ("PraatShell.interactiveSessions", & statistics.interactiveSessions);
	Resources_addString ("PraatShell.dateOfFirstSession", & statistics.dateOfFirstSession [0]);
	Resources_addDouble ("PraatShell.memory", & statistics.memory);
}

void praat_statistics_prefsChanged (void) {
	if (! statistics.dateOfFirstSession [0]) {
		time_t today = time (NULL);
		char *newLine;
		strcpy (statistics.dateOfFirstSession, ctime (& today));
		newLine = strchr (statistics.dateOfFirstSession, '\n');
		if (newLine) *newLine = '\0';
	}
	if (praat.batch)
		statistics.batchSessions += 1;
	else
		statistics.interactiveSessions += 1;
}

void praat_statistics_exit (void) {
	statistics.memory += Melder_allocationSize ();
}

static void sprintVeryLongInteger (char *text, double veryLongInteger) {
	int terabytes, gigabytes, megabytes, kilobytes, bytes;
	terabytes = floor (veryLongInteger / 1e12);
	veryLongInteger -= terabytes * 1e12;
	gigabytes = floor (veryLongInteger / 1e9);
	veryLongInteger -= gigabytes * 1e9;
	megabytes = floor (veryLongInteger / 1e6);
	veryLongInteger -= megabytes * 1e6;
	kilobytes = floor (veryLongInteger / 1e3);
	veryLongInteger -= kilobytes * 1e3;
	bytes = veryLongInteger;
	text [0] = '\0';
	if (terabytes) {
		sprintf (text, "%d,", terabytes);
		sprintf (text + strlen (text), "%03d,", gigabytes);
		sprintf (text + strlen (text), "%03d,", megabytes);
		sprintf (text + strlen (text), "%03d,", kilobytes);
		sprintf (text + strlen (text), "%03d", bytes);
	} else if (gigabytes) {
		sprintf (text, "%d,", gigabytes);
		sprintf (text + strlen (text), "%03d,", megabytes);
		sprintf (text + strlen (text), "%03d,", kilobytes);
		sprintf (text + strlen (text), "%03d", bytes);
	} else if (megabytes) {
		sprintf (text, "%d,", megabytes);
		sprintf (text + strlen (text), "%03d,", kilobytes);
		sprintf (text + strlen (text), "%03d", bytes);
	} else if (kilobytes) {
		sprintf (text, "%d,", kilobytes);
		sprintf (text + strlen (text), "%03d", bytes);
	} else {
		sprintf (text, "%d,", bytes);
	}
}

#ifdef macintosh
	#include <Memory.h>
#endif
void praat_memoryInfo (void) {
	char memoryText [40], allocationText [40];
	#ifdef macintosh
		long grow;
		char freeText [40], contiguousText [40];
	#endif
	sprintVeryLongInteger (memoryText, statistics.memory + Melder_allocationSize ());
	sprintVeryLongInteger (allocationText, Melder_allocationSize ());
	Melder_clearInfo ();
	Melder_info ("Currently allocated: %ld\n   Things: %ld (objects in list: %d)\n   Arrays: %ld",
		Melder_allocationCount () - Melder_deallocationCount (),
		Thing_getTotalNumberOfThings (),
		(int) praat.n,
		NUM_getTotalNumberOfArrays ());
	#ifdef macintosh
	if (Melder_systemVersion < 0x0A00) {
		sprintVeryLongInteger (freeText, FreeMem ());
		sprintVeryLongInteger (contiguousText, MaxMem (& grow));
		Melder_info ("\nFree memory: %s bytes (contiguous: %s bytes)\n", freeText, contiguousText);
	}
	#endif
	Melder_info (
		"\nHistory of this session:\n"
		"   Allocation: %ld (%s bytes)\n"
		"   Deallocation: %ld\n"
		"\nHistory of all sessions from %s until today:\n"
		"   Interactive sessions: %ld, batch sessions: %ld\n"
		"   Total memory use: %s bytes\n"
		,
	#ifdef macintosh
	#endif
		Melder_allocationCount (), allocationText,
		Melder_deallocationCount (),
		statistics.dateOfFirstSession, statistics.interactiveSessions, statistics.batchSessions,
		memoryText);
}

/* End of file praat_statistics.c */

