Commit Graph

33 Commits

Author SHA1 Message Date
Joris Vink 4718bae098 Initial lua runtime.
Works enough so one can do basic configuration and handle HTTP.
2023-01-21 23:41:35 +01:00
Joris Vink a421e7a9cd Add memory protection with KORE_MEM_GUARD.
When KORE_MEM_GUARD is set in the environment when Kore is started
it will enable a few memory protection techniques for all kore pools:

1) The metadata is placed away from the actual user pointer returned.

2) Each entry in a pool is placed in such a way that it is followed
   immediately by a guard page which has PROT_NONE. Accessing a guard
   page will cause an immediate crash.

3) Each entry is marked with PROT_NONE until it is allocated. Once it
   is returned to a pool it becomes PROT_NONE again, protecting against
   use after frees.

This commit also removes the magic goo from the mem facitilies such
as kore_malloc and friends and moves these as canaries into the kore
pool facilities instead.

Note that using this will increase memory pressure and decrease performance.

It is recommended to enable this during development to catch bugs.
2023-01-05 22:47:29 +01:00
Joris Vink a2d312d0a0 kore_debug() has been unused for years.
Kill all useless messages, convert useful ones into kore_log() instead.
2022-08-18 15:20:55 +02:00
Daniel Fahlgren 5e21de3a4c Add printf format attributes and fix fallout 2022-08-17 13:16:18 +02:00
Joris Vink 833ca646e7 i forgot, it's 2022. 2022-01-31 22:02:06 +01:00
Joris Vink cf6cf5f820 Always align pool element lengths properly.
Enforce 8-byte alignment on the pool element lengths.
2021-12-11 22:35:37 +01:00
Joris Vink cef5ac4003 bump copyright year. 2021-01-11 23:46:08 +01:00
Joris Vink 9d0aef0079 bump copyright 2020-02-10 14:47:33 +01:00
Joris Vink eb9b7f7b14 explicitly include sys/types.h
some smaller libc variants do not include this from sys/param.h.
2019-03-06 09:29:46 +01:00
Joris Vink bf1e8e5ffb bump copyright to 2019 2019-02-22 16:57:28 +01:00
Joris Vink 062c80c1ac start enforcing a mininum number of elms in pools. 2018-10-30 10:41:49 +01:00
Joris Vink 71c145932c grow kore_pools at a slower rate.
Before we just doubled in size the second we exhausted a pool instead
of doing a more controlled expansion.

Now we will expand at 25% of the initial elm count whenever we need to.

Will help with memory pressure in certain scenarios.
2018-10-30 10:36:18 +01:00
Joris Vink 45260b491b finally got fed up with the "is exhausted" spam. 2018-04-10 14:11:06 +02:00
Joris Vink cb4567683a use MAP_FAILED. 2018-04-04 09:34:18 +02:00
Joris Vink dd2dff2318 Rework HTTP and worker processes.
The HTTP layer used to make a copy of each incoming header and its
value for a request. Stop doing that and make HTTP headers zero-copy
all across the board.

This change comes with some api function changes, notably the
http_request_header() function which now takes a const char ** rather
than a char ** out pointer.

This commit also constifies several members of http_request, beware.

Additional rework how the worker processes deal with the accept lock.

Before:
	if a worker held the accept lock and it accepted a new connection
	it would release the lock for others and back off for 500ms before
	attempting to grab the lock again.

	This approach worked but under high load this starts becoming obvious.

Now:
	- workers not holding the accept lock and not having any connections
	  will wait less long before returning from kore_platform_event_wait().

	- workers not holding the accept lock will no longer blindly wait
	  an arbitrary amount in kore_platform_event_wait() but will look
	  at how long until the next lock grab is and base their timeout
	  on that.

	- if a worker its next_lock timeout is up and failed to grab the
	  lock it will try again in half the time again.

	- the worker process holding the lock will when releasing the lock
	  double check if it still has space for newer connections, if it does
	  it will keep the lock until it is full. This prevents the lock from
	  bouncing between several non busy worker processes all the time.

Additional fixes:

- Reduce the number of times we check the timeout list, only do it twice
  per second rather then every event tick.
- Fix solo worker count for TLS (we actually hold two processes, not one).
- Make sure we don't accidentally miscalculate the idle time causing new
  connections under heavy load to instantly drop.
- Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
Joris Vink 548348f553 2018 2018-01-20 22:51:06 +01:00
Joris Vink ace8c4e80c Add asynchronous pgsql query support to python.
This commit adds the ability to use python "await" to suspend
execution of your page handler until the query sent to postgresql
has returned a result.

This is built upon the existing asynchrous query framework Kore had.

With this you can now write stuff like:

async def page(req):
	result = await req.pgsql("db", "SELECT name FROM table");
	req.response(200, json.dumps(result).encode("utf-8"))

The above code will fire off a query and suspend itself so Kore can
take care of business as usual until the query is successful at which
point Kore will jump back into the handler and resume.

This does not use threading, it's purely based on Python's excellent
coroutines and generators and Kore its built-in pgsql support.
2017-02-06 11:42:53 +01:00
Joris Vink 33c0b3c753 Make pools thread-safe if built with TASKS.
Use CAS to implement primitive spinlock ontop of the pool
if we have been built with TASKS=1.

This allows tasks to safely use kore_malloc() and kore_free()
since those are now backed by pools.
2016-07-12 16:30:57 +02:00
Joris Vink 3c43d1203c Move towards size_t in the pool API. 2016-07-12 14:01:02 +02:00
Joris Vink 4ad50caa29 Large changes to the memory subsystem in kore.
- Change pools to use mmap() for allocating regions.
- Change kore_malloc() to use pools for commonly sized objects.
  (split into multiple of 2 buckets, starting at 8 bytes up to 8192).
- Rename kore_mem_free() to kore_free().

The preallocated pools will hold up to 128K of elements per block size.

In case a larger object is to be allocated kore_malloc() will use
malloc() instead.
2016-07-12 13:54:14 +02:00
Joris Vink 82a9e6ef59 Formatting and unbreaking NOHTTP builds. 2016-02-01 20:02:02 +01:00
Stig Telfer 2ac6e7d41d Merge branch 'master' into oneswig 2016-01-24 13:46:38 +00:00
Stig Telfer 18d3cc032d rename *_fini to *_cleanup 2016-01-04 21:40:14 +00:00
Joris Vink c4b1206ae3 Bump copyright to 2016. 2016-01-04 12:58:51 +01:00
Stig Telfer 0c51d9da53 Add resource management as part of the kore shutdown process. 2015-12-29 19:39:39 +00:00
Joris Vink 769c78a6e8 Introduce NOHTTP=1 build option.
This basically turns off the HTTP layer for Kore. It does not
compile in anything for HTTP.

This allows Kore to be used as a network application platform as well.
Added an example for this called nohttp.

Other changes that sneaked in while hacking on this:
* Use calloc(), kill pendantic malloc option.
* Killed off SPDY/3.1 support completely, will be superseded by http2

Note that comes with massive changes to a lot of the core API
functions provided by Kore, these might break your application.
2015-11-27 16:22:50 +01:00
Joris Vink af865abede Bump copyright to 2015 2015-04-07 13:08:26 +02:00
Joris Vink 693a07250f bump copyright years 2014-10-14 16:18:23 +02:00
Joris Vink 7b9b1d5c2c sprinkle more const chars 2014-08-04 12:40:21 +02:00
Joris Vink d98d56fb20 Add KORE_PENDANTIC_MALLOC option.
This option tells Kore to zero out memory when allocated, freed or
when get/put from the pools.
2014-04-22 12:46:23 +02:00
Joris Vink d40fd489f8 s/ptr/entry 2014-03-06 08:21:58 +01:00
Joris Vink 69f26823b9 Double check the element is free in kore_pool_get(). 2014-03-06 08:20:05 +01:00
Joris Vink 29fa49ba83 Add fixed size memory pools and use them throughout Kore. 2013-07-15 11:06:36 +02:00