LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2020, 10:06 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
how does Python get the time()?


i put together this short script in Python:
Code:
import time
for _ in range(8):
    print(int(time.time()*3906250))
then i ran it on Xubuntu 18.04 on Python 3.6 under strace and got:
Code:
...
stat("tt.py", {st_mode=S_IFREG|0444, st_size=67, ...}) = 0
openat(AT_FDCWD, "tt.py", O_RDONLY)     = 3
ioctl(3, FIOCLEX)                       = 0
fstat(3, {st_mode=S_IFREG|0444, st_size=67, ...}) = 0
fstat(3, {st_mode=S_IFREG|0444, st_size=67, ...}) = 0
lseek(3, 0, SEEK_SET)                   = 0
read(3, "import time\nfor _ in range(8):\n "..., 45) = 45
read(3, "time.time()*3906250))\n", 4096) = 22
close(3)                                = 0
stat("tt.py", {st_mode=S_IFREG|0444, st_size=67, ...}) = 0
readlink("tt.py", 0x7fff6a7bced0, 4096) = -1 EINVAL (Invalid argument)
getcwd("/home/forums", 4096)            = 13
lstat("/home/forums/tt.py", {st_mode=S_IFREG|0444, st_size=67, ...}) = 0
openat(AT_FDCWD, "tt.py", O_RDONLY)     = 3
fcntl(3, F_GETFD)                       = 0
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
fstat(3, {st_mode=S_IFREG|0444, st_size=67, ...}) = 0
ioctl(3, TCGETS, 0x7fff6a7cde50)        = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0444, st_size=67, ...}) = 0
read(3, "import time\nfor _ in range(8):\n "..., 4096) = 67
lseek(3, 0, SEEK_SET)                   = 0
brk(0x29b5000)                          = 0x29b5000
read(3, "import time\nfor _ in range(8):\n "..., 4096) = 67
read(3, "", 4096)                       = 0
brk(0x29ac000)                          = 0x29ac000
brk(0x29aa000)                          = 0x29aa000
close(3)                                = 0
openat(AT_FDCWD, "/usr/share/zoneinfo/America/New_York", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=3545, ...}) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=3545, ...}) = 0
read(3, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\5\0\0\0\5\0\0\0\0"..., 4096) = 3545
lseek(3, -2261, SEEK_CUR)               = 1284
read(3, "TZif2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\5\0\0\0\5\0\0\0\0"..., 4096) = 2261
close(3)                                = 0
write(1, "6226917864829328\n", 176226917864829328
)      = 17
write(1, "6226917864829508\n", 176226917864829508
)      = 17
write(1, "6226917864829629\n", 176226917864829629
)      = 17
write(1, "6226917864829744\n", 176226917864829744
)      = 17
write(1, "6226917864829858\n", 176226917864829858
)      = 17
write(1, "6226917864829970\n", 176226917864829970
)      = 17
write(1, "6226917864830081\n", 176226917864830081
)      = 17
write(1, "6226917864830191\n", 176226917864830191
)      = 17
brk(0x29a8000)                          = 0x29a8000
rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f81e9921f20}, {sa_handler=0x62ffc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f81e9921f20}, 8) = 0
sigaltstack(NULL, {ss_sp=0x28db540, ss_flags=0, ss_size=8192}) = 0
sigaltstack({ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}, NULL) = 0
exit_group(0)                           = ?
+++ exited with 0 +++
lt2a/forums /home/forums 27> cat tt.py
import time
for _ in range(8):
    print(int(time.time()*3906250))
lt2a/forums /home/forums 28>
it looks like no syscall is needed to get the time. what C library call can do that? or is it just a special memory access location?
 
Old 07-06-2020, 10:33 PM   #2
tinfoil3d
Member
 
Registered: Apr 2020
Location: Japan/RJCC
Distribution: debian, lfs, whatever else i need in qemu
Posts: 268

Rep: Reputation: 75
Confirmed on debian too. Peculiar. If you add time.sleep(1) before that it'll use select(0,0,0,0,[1,0]) instead of nanosleep in linux. But where does it pull the time in clock_gettime/gettimeofday format i wonder?
 
Old 07-06-2020, 10:39 PM   #3
tinfoil3d
Member
 
Registered: Apr 2020
Location: Japan/RJCC
Distribution: debian, lfs, whatever else i need in qemu
Posts: 268

Rep: Reputation: 75
If you run it with ltrace(warning: LOTS of data would be printed) you can actually see clock_gettime() there BUT it's not coming up in strace, which doesn't seem right.
 
Old 07-06-2020, 11:58 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,130

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Code:
man vdso
 
5 members found this post helpful.
Old 07-07-2020, 12:34 AM   #5
tinfoil3d
Member
 
Registered: Apr 2020
Location: Japan/RJCC
Distribution: debian, lfs, whatever else i need in qemu
Posts: 268

Rep: Reputation: 75
Quote:
Originally Posted by syg00 View Post
Code:
man vdso
Thanks man, learned something new today. I thought it's gotta be somehow mapped onto memory otherwise it can't be explained but I've never heard of vdso before.
 
Old 07-07-2020, 04:05 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
It's, clock_gettime, gettimeofday, etc.

https://github.com/python/cpython/bl.../pytime.c#L695
 
Old 07-07-2020, 04:24 PM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 07-10-2020, 04:47 PM   #8
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
i read man vdso but i didn't get much out of it besides it saying strace won't show it. i'm still curious how this code manages to get the time value. i assume it does not do that expensive INT (or its equivalent on other architectures). is it doing a virtual memory trap and code in the kernels handles a vdso memory trap specially to copy the time value into some designated place like a place in the vdso or some register?
 
Old 07-10-2020, 07:35 PM   #9
tinfoil3d
Member
 
Registered: Apr 2020
Location: Japan/RJCC
Distribution: debian, lfs, whatever else i need in qemu
Posts: 268

Rep: Reputation: 75
Yeah, that's what it basically says. You'll have to study vdso code to figure out more but it's only natural it's some kind of map that's probably doesn't have any overhead until queried to be read out from userlevel.
 
Old 07-11-2020, 08:42 AM   #10
Skaperen
Senior Member
 
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,684

Original Poster
Blog Entries: 31

Rep: Reputation: 176Reputation: 176
then that method should be used for all syscalls.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
LXer: Get the ultimate Python resource guide with the Python Book, out now! LXer Syndicated Linux News 0 06-26-2015 01:12 AM
Python - How to get text file data into an array on python. golmschenk Programming 4 11-11-2013 09:15 AM
how to understand user time, sys time, wait time, idle time of CPU guixingyi Linux - Server 1 08-24-2010 10:10 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration