/*
!===============================================================================
!
! c_util.c - C utilities
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2007 Hinnerk Stueben
!               2012 Yoshifumi Nakamura
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD 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.
!
! BQCD 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 BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
*/
# include <stdio.h>
# include <stdarg.h>
# include "c_util.h"
# include "c_defs.h"

/*---------------------------------------------------------------------------*/
void c_die(char *file, int line, char *fmt, ...)
{
  fprintf(stderr, "%s (%d): ", file, line);

  va_list args;
  va_start(args, fmt);
  vfprintf(stderr, fmt, args);
  fprintf(stderr, "\n");
  va_end(args);

  ABBRUCH();
}

/*---------------------------------------------------------------------------*/
void c_warn(char *file, int line, char *fmt, ...)
{
  fprintf(stderr, "WARNING: %s (%d): ", file, line);

  va_list args;
  va_start(args, fmt);
  vfprintf(stderr, fmt, args);
  fprintf(stderr, "\n");
  va_end(args);
}

/*---------------------------------------------------------------------------*/
#include <time.h>
//#include <stdio.h>

// get nano second time
// needs  "-lrt" option 
 
//int main() {
//  struct timespec now;
//  struct tm *t;
//  clock_gettime(CLOCK_REALTIME, &now);
//  t = localtime(&now.tv_sec);
// 
//  printf("%04d/%02d/%02d %02d:%02d:%02d.%09ld\n",
//       t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
//       t->tm_hour, t->tm_min, t->tm_sec,
//       now.tv_nsec);
//  return 0;
//}

//double
//GET_CLOCK (void)
//{
//  struct timespec now;
//  clock_gettime(CLOCK_REALTIME, &now);
//  return ((double) now.tv_sec + (double) now.tv_nsec * 1e-9);
//}

/*===========================================================================*/
#include <sys/resource.h>

void c_stack ()
{
  const rlim_t kStackSize = 64L * 1024L * 1024L * 1024L;   // min stack size = 1 Gb
  struct rlimit rl;
  int result;
  int pe = MY_PE();

  result = getrlimit(RLIMIT_STACK, &rl);
  if (result == 0) {
    if (rl.rlim_cur != RLIM_INFINITY) {
      if (pe == 0)fprintf(stderr, "soft limit (rl.rlim_cur) = %lld, INFINITY = -1\n", (long long)rl.rlim_cur);
      if (rl.rlim_cur < kStackSize){
	//rl.rlim_cur = kStackSize;
	rl.rlim_cur = RLIM_INFINITY;
	result = setrlimit(RLIMIT_STACK, &rl);
	if (pe == 0)fprintf(stderr, "setrlimit returned result = %d\n", result);
	if (result != 0)fprintf(stderr, "setrlimit returned result = %d\n", result);
      }
      result = getrlimit(RLIMIT_STACK, &rl);
      if (pe == 0)fprintf(stderr, "soft limit (rl.rlim_cur) = %lld, INFINITY = -1\n", (long long)rl.rlim_cur);
    }
  } else {
    if (pe == 0)fprintf(stderr, "getrlimit returned result = %d\n", result);
  }
  // ...
}
