0

I'm trying to correlate the timing information obtain from a java job and linux performance monitoring tool perf (specifically perf stat).

The timing information from java is obtained using String tstamp0 = String.valueOf(System.currentTimeMillis()); (This is essentially time in milliseconds from epoch) whereas perf gives the time the process has began and the subsequent recording only show the time elapsed.

What I would like to do is, convert the timing information obtained from the perf stat to milliseconds, and here is where I'm failing. I'm approaching this problem in Python.

This piece of code is giving me the timing information from perf

tailit = "head -n 1 " + dataset_path
process = subprocess.Popen(tailit, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
date_time = out.split("\n")[0].split(" ")[4:]
date = date_time[3] + "-" + date_time[0] + "-" +  date_time[1]
time = date_time[2]
#TIMESTAMP

INIT_TIME = datetime.datetime.strptime(date + ' ' + time, "%Y-%B-%d %H:%M:%S") + datetime.timedelta(seconds=0.01)
#df is pandas data frame
df['STAMPME'] = df['TCOUNT'].apply(lambda x: foobar(datetime.timedelta(seconds=x) + INIT_TIME))

here foobar is the following to convert a string to timestamp in milliseconds, but it doesn't make sense.

def foobar(INIT_TIME):
    d = datetime.datetime.strptime(str(INIT_TIME), "%Y-%m-%d %H:%M:%S.%f").strftime('%s')
    d_in_ms = int(d)*1000
    return (d_in_ms)

Any help will be appreciated.

EDIT: Prior questions were not addressing the problem of correlating the java timestamp (currentTimeMillis()) to the datetime with milliseconds.

For instance: with the function foobar: with INIT_TIME set as 2017-05-11 10:56:54.203, the return value is 1494493014000 when it instead should be 1494500214203

tandem
  • 2,040
  • 4
  • 25
  • 52
  • Possible duplicate of [Convert python datetime to epoch with strftime](http://stackoverflow.com/questions/11743019/convert-python-datetime-to-epoch-with-strftime) – luoluo May 12 '17 at 07:49
  • @luoluo I tried that... but didn't help. – tandem May 12 '17 at 07:53
  • In what way didn’t help? How was it insufficient of failed? – Ole V.V. May 12 '17 at 07:53
  • It doesn't show to how convert a datetime to timestamp milliseconds.. (similar to how currentTimeMillis in Java does.) Also tried this:http://stackoverflow.com/questions/41635547/convert-python-datetime-to-timestamp-in-milliseconds – tandem May 12 '17 at 07:55
  • @OleV.V.: I gave an example above now – tandem May 12 '17 at 08:04
  • `>>> (datetime.datetime(2012,04,01,0,0) - datetime.datetime(1970,1,1)).total_seconds() ` – luoluo May 12 '17 at 08:07

1 Answers1

1

I think figured out the problem. Looks like foobar function is returning time in GMT+2, whereas the java job was returning time in GMT. so with a timedelta of +2, I could solve it.

tandem
  • 2,040
  • 4
  • 25
  • 52