Giter Club home page Giter Club logo

nimgo's Issues

Reuse raw protected memory (in remplacement of coroutinepool implementation)

On posix: Deallocation of virtual memory allocated with mmap by a factor >10 slower than virtual memory allocated with alloc. But memory allocated with alloc is incompatible with mprotect

proc mcoDeallocator(p: pointer, size: uint, allocatorData: ptr CoroutineObj) {.cdecl.} =
when NimGoNoVMem:
dealloc(p)
else:
if munmap(p, size.int) != 0:
raiseOSError(osLastError())
when not NimGoNoDebug:
unrecordProtectedPage(allocatorData)
proc mcoAllocator(size: uint, allocatorData: ptr CoroutineObj): pointer {.cdecl.} =
when NimGoNoVMem:
result = alloc0(size)
else:
result = mmap(nil, size.int,
bitor(PROT_READ, PROT_WRITE),
bitor(MAP_PRIVATE, MAP_ANONYMOUS),
-1, 0
)
if result == MAP_FAILED:
raiseOSError(osLastError())
if mprotect(result, PageSize, PROT_NONE) != 0:
## Stack begins at its bottom
mcoDeallocator(result, size, nil)
raiseOSError(osLastError())
when not NimGoNoDebug:
recordProtectedPage(allocatorData, result)

@Araq Do you have some knowledge of those memory internals that could explain why alloc is so much faster but incompatible with PROT_NONE pages ? That order of magnitude is not negligible

Prevent stackoverflow

Handling stack memory has multiple aspects into achieving a reliable, secure, and explicit error management.

Growable stack

This might not be easy to implement. So the best solution is certainly to rely on exclusively on having a very big stack of virtual memory. There is still the problematic to support lower end device, so we shall determine the maximum amount of storage to allocate (for example sysconf(SC_PAGESIZE) * (sysconf(SC_PHYS_PAGES) on linux) and to support operating systems that can't support virtual memory (which ones ?)

Protection against overflow

The last allocated page for the stack should be marked as protected:

  • m_protect on linux
  • VirtualProtect on windows

Error message in case of stackoverflow

When a page of memory is protected, the OS will throw an exception like SIGESEGV on linux. This can be catched but cannot be recovered safely (at least on linux). So SIGSEGV shall be catched to permit to write the stacktrace, the error message. However, SIGEGV from stackoverflow should be distinguished from other SIGSEGV (how ?)

Some resources:

Testing if NimGo test suite pass in windows

If you have time, don't hesitate to run the test suites and the bench on windows with NimGo and tell me what are the errors messages.

If you know how and it is possible, you can also tell me how to add a windows github hosted runner

Any proposition of fix are also welcome ;-)

Rename the NimGo library

A user in the Nim Forum suggests to rename NimGo for a better name. I think he might be right.

Here is his post https://forum.nim-lang.org/t/11720#75214

I think NimGoroutines shall be a good name. It expresses both the concept of Coroutines, the inspiration of Go in that specific feature (Goroutines), and is not restricted to only IO (because NimGo will also leverage Os Threads in future release).

Open reflexions to implement a M:N multi threaded dispatcher on NimGo

Please read here the definitions I use before responding to this post. If we don't agree on what we talk, we won't talk about much.

Questions

  1. Should the EventLoop and DispatcherLoop be merged together ?
  2. If they are separate, should the EventLoop run in its own thread or each thread would run a shared EventLoop ?
  3. Should each thread have its own task Dispatcher in addition to global task Dispatcher ?
  4. Should it have a fixed number of Threads ? If yes, how many ? If no, determined by what ?
  5. What design should the DispatcherLoop have ?
  • FIFO for all tasks, except LIFO for last task for better caching/responsivness ?
  • FIFO only ?
  1. Other considerations ?

Proposition of response

If I read correctly this paper shared from @mratsim : https://assets.ctfassets.net/oxjq45e8ilak/48lwQdnyDJr2O64KUsUB5V/5d8343da0119045c4b26eb65a83e786f/100545_516729073_DMITRII_VIUKOV_Go_scheduler_Implementing_language_with_lightweight_concurrency.pdf

The answers could be :

  1. No
  2. Separate thread for I/O pool
  3. Yes
  4. Should have 61 Os Threads (why ?)
  5. FIFO with LIFO for last item

However I am wondering how well a single selector can scale. But having multiple selectors by thread is trickier, even if the same fd can be registered in multiple dispatcher (with special considerations for thread safety, but this might be implementation specific).

Redesign the design of the selector pool to make it work in windows.

This is the last major work before work on multithreading (#21) could begin.

The goal is to create a portable API to query asynchronous events. This will likely significantly redesign the public API of the nimgo/eventdispatcher library, and hopefully the API won't require many changes after this update.

Completing goproc module

  • defining the API
  • Writing startProcess for posix
  • Handle the pseudo terminal specific case for posix
  • Add convenience function run
  • Implements the window's low level API (help wanted)
  • etc?

Rename goAsync keyword

Because the library won't be strictly async, the keyword named `goAsync might not be appropriate ?

Here are some propositions:

  • go
  • goTask
  • goAsync
  • goNim
  • launch
  • startTask
  • coSpawn

Convert 4-space indent to 2-space indent

Although I highly prefer four spaces ident, this is the norm in Nim and it will facilitate contributions. I have modified my VsCodium settings to help me see them better.

gostdin/gostdout/gostederr aren't registered correctly

import nimgo, nimgo/gofile

var file = currentSourcePath()
var gf = openGoFile(file)

withEventLoop():
    goAsync proc() =
        echo gostdin.readline()

src/nimgo/eventdispatcher.nim(424) addInsideSelector
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Changing how Coroutine's internal struct is allocated

Coroutine ref object in fact wraps a McoCoroutine struct defined in minocoro.h This struct holds low level information of the coroutine. It uses calloc/free. It should be allocated instead on Nim's side with Nim's alloc/dealloc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.