/*****************************************************************************\
 *  timers.c - Timer functions
 *****************************************************************************
 *  Copyright (C) 2002-2006 The Regents of the University of California.
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 *  Written by Morris Jette <jette@llnl.gov>
 *  CODE-OCEC-09-009. All rights reserved.
 *
 *  This file is part of SLURM, a resource management program.
 *  For details, see <http://slurm.schedmd.com/>.
 *  Please also read the included file: DISCLAIMER.
 *
 *  SLURM 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.
 *
 *  In addition, as a special exception, the copyright holders give permission
 *  to link the code of portions of this program with the OpenSSL library under
 *  certain conditions as described in each individual source file, and
 *  distribute linked combinations including the two. You must obey the GNU
 *  General Public License in all respects for all of the code used other than
 *  OpenSSL. If you modify file(s) with this exception, you may extend this
 *  exception to your version of the file(s), but you are not obligated to do
 *  so. If you do not wish to do so, delete this exception statement from your
 *  version.  If you delete this exception statement from all source files in
 *  the program, then also delete it here.
 *
 *  SLURM 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 SLURM; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
\*****************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include "src/common/log.h"

/*
 * slurm_diff_tv_str - build a string showing the time difference between two
 *		       times
 * IN tv1 - start of event
 * IN tv2 - end of event
 * OUT tv_str - place to put delta time in format "usec=%ld"
 * IN len_tv_str - size of tv_str in bytes
 * IN from - where the function was called form
 */
extern void slurm_diff_tv_str(struct timeval *tv1, struct timeval *tv2,
			      char *tv_str, int len_tv_str, char *from,
			      long limit, long *delta_t)
{
	char p[64] = "";
	struct tm tm;

	(*delta_t)  = (tv2->tv_sec  - tv1->tv_sec) * 1000000;
	(*delta_t) +=  tv2->tv_usec - tv1->tv_usec;
	snprintf(tv_str, len_tv_str, "usec=%ld", *delta_t);
	if (from) {
		if (!limit)
			limit = 1000000;
		if (*delta_t > limit) {
			if (!localtime_r(&tv1->tv_sec, &tm))
				fprintf(stderr, "localtime_r() failed\n");
			if (strftime(p, sizeof(p), "%T", &tm) == 0)
				fprintf(stderr, "strftime() returned 0\n");
			verbose("Warning: Note very large processing "
				"time from %s: %s began=%s.%3.3d",
				from, tv_str, p, (int)(tv1->tv_usec / 1000));
		}
	}
}

/* block_daemon()
 *
 * This function allows to block any daemon
 * in a specific function. Once the daemon
 * is block gdb can be attached and by resetting
 * the block variable restored to normal operation.
 */
extern void
block_daemon(void)
{
	int block;

	block = 1;
	while (block == 1) {
		info("%s: attachme, attachme...", __func__);
		sleep(2);
	}
}
