diff options
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/math.nim | 21 | ||||
-rw-r--r-- | lib/pure/nimprof.nim | 13 | ||||
-rw-r--r-- | lib/pure/selectors.nim | 36 |
3 files changed, 49 insertions, 21 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index e4aecd272..78ea02cbf 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -135,12 +135,12 @@ proc random*(max: int): int {.gcsafe.} ## which initializes the random number generator with a "random" ## number, i.e. a tickcount. -when not defined(windows): - proc random*(max: float): float {.gcsafe.} - ## returns a random number in the range 0..<max. The sequence of - ## random number is always the same, unless `randomize` is called - ## which initializes the random number generator with a "random" - ## number, i.e. a tickcount. This is currently not supported for windows. +proc random*(max: float): float {.gcsafe.} + ## returns a random number in the range 0..<max. The sequence of + ## random number is always the same, unless `randomize` is called + ## which initializes the random number generator with a "random" + ## number, i.e. a tickcount. This has a 16-bit resolution on windows + ## and a 48-bit resolution on other platforms. proc randomize*() {.gcsafe.} ## initializes the random number generator with a "random" @@ -205,7 +205,14 @@ when not defined(JS): proc drand48(): float {.importc: "drand48", header: "<stdlib.h>".} proc random(max: float): float = result = drand48() * max - + when defined(windows): + proc random(max: float): float = + # we are hardcodeing this because + # importcing macros is extremely problematic + # and because the value is publicly documented + # on MSDN and very unlikely to change + const rand_max = 32767 + result = (float(rand()) / float(rand_max)) * max proc randomize() = randomize(cast[int](epochTime())) diff --git a/lib/pure/nimprof.nim b/lib/pure/nimprof.nim index 3d0cc2154..ab7cd1944 100644 --- a/lib/pure/nimprof.nim +++ b/lib/pure/nimprof.nim @@ -58,8 +58,9 @@ when not defined(memProfiler): ## instruction count measure instead then. if intervalInUs <= 0: interval = 0 else: interval = intervalInUs * 1000 - tickCountCorrection - + when withThreads: + import locks var profilingLock: TLock @@ -72,7 +73,7 @@ proc hookAux(st: TStackTrace, costs: int) = var last = high(st) while last > 0 and isNil(st[last]): dec last var h = hash(pointer(st[last])) and high(profileData) - + # we use probing for maxChainLen entries and replace the encountered entry # with the minimal 'total' value: if emptySlots == 0: @@ -133,7 +134,7 @@ else: hookAux(st, 1) elif getticks() - t0 > interval: hookAux(st, 1) - t0 = getticks() + t0 = getticks() proc getTotal(x: ptr TProfileEntry): int = result = if isNil(x): 0 else: x.total @@ -145,7 +146,7 @@ proc `//`(a, b: int): string = result = format("$1/$2 = $3%", a, b, formatFloat(a / b * 100.0, ffDefault, 2)) proc writeProfile() {.noconv.} = - when defined(system.TStackTrace): + when defined(system.TStackTrace): system.profilerHook = nil const filename = "profile_results.txt" echo "writing " & filename & "..." @@ -156,7 +157,7 @@ proc writeProfile() {.noconv.} = var entries = 0 for i in 0..high(profileData): if profileData[i] != nil: inc entries - + var perProc = initCountTable[string]() for i in 0..entries-1: var dups = initSet[string]() @@ -166,7 +167,7 @@ proc writeProfile() {.noconv.} = let p = $procname if not containsOrIncl(dups, p): perProc.inc(p, profileData[i].total) - + var sum = 0 # only write the first 100 entries: for i in 0..min(100, entries-1): diff --git a/lib/pure/selectors.nim b/lib/pure/selectors.nim index f630ba235..498f41e83 100644 --- a/lib/pure/selectors.nim +++ b/lib/pure/selectors.nim @@ -49,9 +49,10 @@ when defined(linux) or defined(nimdoc): ## Registers file descriptor ``fd`` to selector ``s`` with a set of TEvent ## ``events``. var event = createEventStruct(events, fd) - if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fd, addr(event)) != 0: - OSError(OSLastError()) - + if events != {}: + if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fd, addr(event)) != 0: + OSError(OSLastError()) + var key = PSelectorKey(fd: fd, events: events, data: data) s.fds[fd] = key @@ -61,11 +62,27 @@ when defined(linux) or defined(nimdoc): events: set[TEvent]): PSelectorKey {.discardable.} = ## Updates the events which ``fd`` wants notifications for. if s.fds[fd].events != events: - var event = createEventStruct(events, fd) + if events == {}: + # This fd is idle -- it should not be registered to epoll. + # But it should remain a part of this selector instance. + # This is to prevent epoll_wait from returning immediately + # because its got fds which are waiting for no events and + # are therefore constantly ready. (leading to 100% CPU usage). + if epoll_ctl(s.epollFD, EPOLL_CTL_DEL, fd, nil) != 0: + OSError(OSLastError()) + s.fds[fd].events = events + else: + var event = createEventStruct(events, fd) + if s.fds[fd].events == {}: + # This fd is idle. It's not a member of this epoll instance and must + # be re-registered. + if epoll_ctl(s.epollFD, EPOLL_CTL_ADD, fd, addr(event)) != 0: + OSError(OSLastError()) + else: + if epoll_ctl(s.epollFD, EPOLL_CTL_MOD, fd, addr(event)) != 0: + OSError(OSLastError()) + s.fds[fd].events = events - s.fds[fd].events = events - if epoll_ctl(s.epollFD, EPOLL_CTL_MOD, fd, addr(event)) != 0: - OSError(OSLastError()) result = s.fds[fd] proc unregister*(s: PSelector, fd: TSocketHandle): PSelectorKey {.discardable.} = @@ -123,7 +140,10 @@ when defined(linux) or defined(nimdoc): ## Determines whether selector contains a file descriptor. if s.fds.hasKey(fd): # Ensure the underlying epoll instance still contains this fd. - result = epollHasFd(s, fd) + if s.fds[fd].events != {}: + result = epollHasFd(s, fd) + else: + result = true else: return false |