4

Is there way to get system time in VCS/UVM ? I am looking for something similar to Perl's localtime(time). Is there way to print system time for every uvm_info printed ?

Jean
  • 21,665
  • 24
  • 69
  • 119

3 Answers3

4

One way is to use $system() to run any system command, including system' date command.

initial
 begin
    $system("date");
 end

From IEEE 1800 LRM:

$system makes a call to the C function system(). The C function executes the argument passed to it as if the argument was executed from the terminal. $system can be called as either a task or a function. When called as a function, it returns the return value of the call to system() with data type int. If $system is called with no string argument, the C function system() will be called with the NULL string.

Also, see here.

Greg
  • 18,111
  • 5
  • 46
  • 68
Ari
  • 7,251
  • 11
  • 40
  • 70
  • $system directly returns the return value of the system call upon exit, but this value is only one to two bytes, and it usually is either 0 or an error code. The common technique for extracting the time in the simulator is to write it in a file as described in the link I posted in my answer. – Ari Sep 29 '14 at 17:40
  • In UVM is it possible to dump the wall clock times spent in each phases ? – Jean Sep 29 '14 at 18:11
  • Looks like you are talking about the time the simulator spent to run each phase. If so, the answer is yes, call `$system` before and after each phase, write the output in a file each time and read it back. Then measure the difference. – Ari Sep 29 '14 at 18:51
4

Depends on the version of VCS you are using. The latest version should support $system as defined in IEEE Std 1800-2012 § 20.18.1. Assuming you are running in a UNIX based environment you can do:

function string get_localtime();    
  int fd;
  string localtime;
  void'($system("data > localtime")); // temp file
  fd = $fopen("localtime", "r");
  void'($fscanf(fd,"%s",localtime));
  $fclose(fd);
  void'($system("rm localtime")); // delete file
  return localtime;
endfunction

If your version VCS doesn't support $system or if your more conformable with C/C++, then use DPI (see IEEE Std 1800-2012 § 35). Create a function in C and compile it in VCS with your SystemVerilog files. In the SystemVerilog, add the import to allow access to your C function. Assuming your method name is my_localtime and and return time is a string, the import should look like:

import "DPI" function string my_localtime();
Greg
  • 18,111
  • 5
  • 46
  • 68
  • 2
    `localtime()` is a function of C library and returns `struct tm *` not a string. Name your own function other than `localtime`, otherwise, the simulator might call wrong function due to the shared library order dependency. – jclin Sep 26 '14 at 03:25
  • Is there a built in DPI function to get time so that I don't have to write one ? – Jean Oct 24 '14 at 17:04
  • @Greg: The linux `date` command output will be something like: `Mon Aug 7 14:00:34 PDT 2017`. When you call `$fscanf`, you are using `%s` as the format, so it'll match a string (which is a sequence of nonwhite space characters, as per the LRM). Since the 4th character is a white space, the `localtime` string will contain only the first 3 characters of the output (i.e., the day of the week). – AndresM Aug 07 '17 at 21:12
1

In c file wallclock.c :

#include <time.h>
wallclock() {
   time_t t;
   t = time(NULL);
   return (ctime(&t));
   //return time(NULL);
}

In SV file :

import "DPI-C" function string wallclock();

module try;

   //int unsigned   t;
   string t;

   initial begin
      t = wallclock();
      $write("time=%0s\n", t);
   end
endmodule
Meir
  • 287
  • 1
  • 4
  • 15