On Sunday, 30 September 2018 01:47:35 UTC+3,
superdupe...@gmail.com wrote:
> On Saturday, September 29, 2018 at 2:31:34 PM UTC-7, Öö Tiib wrote:
> > Then it is unfair to compete with malloc and free that are thread-safe.
> > Have you benchmarked against malloc and free compiled as single-threaded?
>
> I used the latest dlmalloc source (the mother of most if not all
> malloc implementations) which does not have lock enabled by default.
That dlmalloc has thread-safety when USE_MALLOC_LOCK is defined and that
must be defined in multi threaded program unless someone wants to use
it narrowly for managing state that fully belongs to single thread
only.
There are no authorities, mothers and fathers in software development
products. Instead lot of people write and rewrite and repair and break
similar algorithms from year to year.
>
> > That approach could be thinkable if there was clear distinction from
> > malloc and free that are not thread-local. Otherwise how we make
> > difference between things that are made strictly for thread-local
> > processing and things that may end their life-time in hands of some
> > other thread? Having such complications would make it error-prone,
> > but still useful as performance optimization.
>
> An approach is basically have a thread local variables to keep track
> of memory pool including system malloc/free is being used.
> And switch to use specific pool using the flag.
But your cookmem does not do that, it is just totally thread-unsafe?
Also its architecture is not designed to carry described thread
local pools. It could be instrumented with locks at best.
> > This scenario would be only thinkable when threads acted like
> > separate processes, communicating with each other only
> > through rather restricted means like sockets or pipes or
> > memory-mapped files of operating system. It is no way the case
> > with usual multi-threaded programs and it would be likely better
> > to use actual multi-processing and the processes compiled as
> > single-threaded in these scenarios.
>
> Imagine that you are running lots of threads to do some heavy processing.
> Each thread has its own partition of work (which may or may not need
> to call 3rd party library), with some shared data cache, and thread
> specific memory. Now, one thread declares there is an error / solution
> and the job can be terminated early. What do you do at this point?
>
> One possible solution is to gather the output info and suspend and
> kill all the threads and release associated resources. I am not
> saying that this is the only solution, but it certainly is a choice
> that pushes certain difficult aspects of task/resource management
> to specific places, rather than spreading it allover the place.
>
> Now, you could argue that this type of work "should" be done in
> process model. I do not want to get into thread vs process
> debate. However, threads are typically lighter weight, and
> lower synchronization costs. Resource managements are also simpler.
All we talk about is resource management, particularly memory
management here. Indeed it is simpler with thread-safe allocator.
However with thread-unsafe allocator it will be rather complicated
and error-prone. Someone naively using cookmem in their network service
will possibly open it up to wide array of attack vectors.
> > > Obviously, there are other pieces you need to incorporate this memory pool.
> >
> > I did not manage to follow what you meant.
>
> What I meant that you cannot just drop in memory pool and expect
> it to magically solve the problem. It requires certain architectural
> designs such that memory pool provides the benefits. For typical
> non-server applications, one may never need it.
Yes but if global malloc and free are replaced with thread-unsafe
then the tools to control that aspect of architecture are abstracted
away from architect's hands. The fundamental block, the thread-safe
allocator upon what architect can build thread-local pools if needed
is now missing from the landscape.
> For a long running server process, how to avoid memory leak,
> fragmentation, etc are challenging issues. CookMem is intended
> to be a part of solutions, rather than the solution by itself.
I did not ask about other possible programming problems. I was
particularly interested in under what scenario you consider
your thread-unsafe allocator to be viable as part of solution.
We have plenty of other issues to solve, but those are orthogonal
to thread-safety of allocator.