#Zag

1 messages · Page 6 of 1

heavy sandal
#

my slab (basically linux slub) works but its kinda slow so im looking at other ideas lmao

fallen bobcat
#

Magazines are a must

#

if you want performance

#

What I optimized was the slow path

heavy sandal
#

the problem is despite having actually read the papers on this i dont really fully understand what a magazine is here lmao

fallen bobcat
#

magazines are great

#

I have them in my pmm too

heavy sandal
#

(i wasnt just talking about wanting to read through your new opts, i meant the whole thing lol)

fallen bobcat
#

yeah yeah

languid canyon
#

What's that?

fallen bobcat
languid canyon
#

Ah

fallen bobcat
#

@heavy sandal thoughts?

#

imo this is sad because this is clearly an improvement

#

But it fractures the zig community

heavy sandal
fallen bobcat
#

ok but they have a better compiler

heavy sandal
#

also like. what in gods name are they doing that theyre running into compile time issues

heavy sandal
fallen bobcat
#

I am also running into compile time issues though

#

Debug builds for Linux are slow

heavy sandal
#

if it says llvm emit object theres nothing zig can do about that

fallen bobcat
#

I'm not sure, linking in particular is the slowest I think

heavy sandal
#

thats llvm being slow as balls

fallen bobcat
#

Like bun did

heavy sandal
#

bun isnt parallelizing emit object

fallen bobcat
#

You can't claim llvm is slow when clang compiles stuff much quicker

heavy sandal
#

they parallelized sema

fallen bobcat
#

also "multiple codegen units"

#

dunno what that means

heavy sandal
#

that could be the same? idk

#

theres def room for improvement but most zig users nowadays dont even need the llvm backend

#

and most people arent as insane as bun to be hitting time issues in sema of all places

fallen bobcat
#

mhm

heavy sandal
#

yeah

#

i wouldnt trust it and i think a lot of the zig community is the same

fallen bobcat
#

I might try to contribute to the compiler to make it faster

heavy sandal
#

mhm

fallen bobcat
heavy sandal
#

compiler perf is one of the big things people are working on rn so if you manage it then good

#

its just that most of the focus has been on the self-hosted stuff

heavy sandal
#

and the api to emit stuff directly is slow as balls iirc

long pendant
heavy sandal
#

yeah

long pendant
#

and it's not as though parallelising things is a trivial matter

heavy sandal
#

thats why i dont expect most of the community to want to use that fork if thats the code quality

long pendant
#

if they didn't review claude's PRs well enough to at least separate restyling from new features, then you have to wonder what other problems might be lurking

heavy sandal
#

yeah exactly

west talon
#

The changing of magic numbers is also an uhhhh

heavy sandal
#

yeah

long pendant
#

as i have found LLMs are fantastic at producing something that looks like a solution to a problem, but to the trained eye, it becomes immediately clear that outside of the narrow conditions the LLM considered, the solution is usually at least inelegant or restrictive, and often fundamentally broken

languid canyon
#

Yeah thats why you dont ask it to implement features

#

Only specific tiny scope functions etc

long pendant
#

at least for the time being, the only guarantee of quality is a thorough understanding of the architecture of your own program, then you can check whether it's accounted for everything, or not

#

and i would generally be sceptical of commits coming in that are "coauthored by" an LLM

languid canyon
#

I also like to just ask it to review my code and occasionally it points out a bug or two

fallen bobcat
#

I'm thinking LLMs are good to show PoCs

#

Like this

#

Bun could've just implemented a PoC on the compiler that showed it could be greatly optimized

#

and then edit the code to make it higher quality

heavy sandal
#

or use that as guides to write it by hand and better integrated yeah

#

imo if theres any valid case for llms in programming its r&d stuff and POCs. the thing you use to say hey this is proof that this project is worth working on and then you dive into it for real

fallen bobcat
#

yeah I agree

rocky yoke
long pendant
heavy sandal
#

as i said: if theres any use its for proofs of concept or initial idea drafting and then you do the real thing mostly by hand to get it right

long pendant
#

one of the interesting things is that they seem to try to guard against conditions which are virtually impossible to trigger (i.e. they might happen if you plugged a serial console in and opened a hex editor on the live kernel memory, and gave the keyboard to a monkey) which produces an effect of "wow, it's really considering the edge cases" but while ignoring logical faults

rocky yoke
#

yeah, you definitely can't trust it to cover all cases without review

#

it will consider obscure ones without exhausting obvious ones

long pendant
#

it's at least a more sublime way of satisfying the reward function than what they used to do, which was to delete tests that it couldn't get to pass

fallen bobcat
#

I think the next thing I'll add will be turnstiles

heavy sandal
#

turnstiles?

fallen bobcat
#

the allocator would need more kernel infrastructure to get better

fallen bobcat
heavy sandal
#

is it just a thing that lets you release one waiter at a time?

fallen bobcat
#

no

#

it's basically a lock "backend"

#

which also does priority inheritance

heavy sandal
#

so what functions does it have?

fallen bobcat
#

where the address of the lock is hashed and indexed into a global array of turnstiles

heavy sandal
#

ok so thats where it is but what does it do

fallen bobcat
#

or something

#

this is a good overview

#

the locking in particular is a bit tricky to get right

heavy sandal
#

oh its wait handles ok

fallen bobcat
#

I had started to implement them in my C code but was skill issued and didnt have the patience to go through it

heavy sandal
#

ive got something similar called a WaitHandle except i made them fields of the sync object

fallen bobcat
#

I'm not sure thats the same thing

#

also this allows for pointer-sized mutexes which is cool

heavy sandal
#

so is this basically just an encapsulation of the wait queue for a sync object?

#

im pretty sure its just me being stupid but im having trouble understanding still from that article

fallen bobcat
#

if u want

heavy sandal
#

so its an encapsulation of wait queues then, which is what my WaitHandle is (my name is kinda shit i should maybe change it sometime)

fallen bobcat
#

I'm not sure what you mean so I can't really say but I think, yes, broadly

heavy sandal
fallen bobcat
heavy sandal
#

so yeah same idea

fallen bobcat
#

also this

#

this is from the freebsd book

#

I only have that book on my mac

#

i have the solaris one on my desktop

heavy sandal
#

mhm

fallen bobcat
#

the temptation to template the allocator

#

specifically I am wondering if having separate alloc/free magazines is a perf hit if separation is not needed, so I am thinking of templating Zone so it takes a separate_magazines bool

#

eh kinda dont wanna mess with that rn, I'll refactor it when I do the memory stuff

fallen bobcat
#

interesting how much derives from solaris

#

like this is roughly pretty similar to freebsd

heavy sandal
#

mhm

fallen bobcat
#

aaaaa emacs is broken

#

no syntax highlighting wtf

#

nvm I updated and they fixed it

#

weird

fallen bobcat
#

lol @languid canyon the 1 character typo fixer will strike again

#

no two characters even

#

a new record

languid canyon
#

wait ctx?

fallen bobcat
languid canyon
#

whats the context

fallen bobcat
#

but like I kinda wanna PR to fix that typo

#

professional typo fixer

languid canyon
#

i dont even see the typo lmao

fallen bobcat
#

assicated

languid canyon
#

lmao

#

yeah go for it

heavy sandal
#

ah ok yeah i see it now

fallen bobcat
#

freebsd is cool because they accept github PRs now

languid canyon
#

CV:

  • Respected contributor for Linux and FreeBSD, having fixed many typos across all code bases
fallen bobcat
#

bruh i am having a weird bug I didnt notice until now

#

I am testing blocking on events and there is some weird corruption happening

heavy sandal
#

oh hey welcome to my hell of 2-3 weeks ago

fallen bobcat
#

it's weird it never happened before in the fireworks test

#

I'm thinking maybe it has to do with synchronization objects vs notification (like timers)

fallen bobcat
heavy sandal
#

you figure it out?

fallen bobcat
#

well I should have thought about it when writing th ecode

heavy sandal
#

the worst kind of bug

fallen bobcat
#

basically wait should look like this

#

before I fixed it, I called block() inside the cmpxchg success branch and block() is the one holding the thread lock

heavy sandal
#

oh yeah

fallen bobcat
#

now the problem is that this can race since it can be committed without having actually blocked first

heavy sandal
#

so thats uh

#

the exact same issue i was having

#

like 3 weeks ago

fallen bobcat
#

lol fr?

heavy sandal
#

that you sent me that talk about the nt scheduler wait stuff to help me try and fix

#

in reimplementing to have wait aborts i found that bug

#

which was the real issue

fallen bobcat
#

ah

#

well

#

now it works

heavy sandal
#

thats good

fallen bobcat
#

Ithink

#

I may not have fixed it idk

#

that was def a bug

#

well I havent fixed the weird corruption

#

:(

heavy sandal
#

F

fallen bobcat
#

idk why it just straight up crashes into unitinialized memory or something

#

omg I can't write

heavy sandal
#

i still have an issue like that but it only appeared when i did callback handling for io instead of just always using events and i gave up and killed the callback part and always use events now so it doenst happen

fallen bobcat
#

bruh

fallen bobcat
#

ugh this is so tricky

#

what's annoying is that I thought I had tested the scheduler extensively with the fireworks

#

turns out it didnt handle that edge case

past dome
#

or is it

fallen bobcat
#

No idea yet

#

I thought it was a turnstile bug but it turns out it's a bug somewhere in my waiting code or something which causes corruption (probably stack corruption?) which leads to a random jump

fallen bobcat
#

omg I think I know what it is

#

nvm no but I did find a bug

heavy sandal
fallen bobcat
#

forgot to lock the thread when stealing it in the idle thread

#

that was a bug

heavy sandal
#

ah

fallen bobcat
#

I'm sure the bug is some locking bug on the thread i just dont know where yet

fallen bobcat
#

ok another bug I fixed

#

hopefully that was it

heavy sandal
#

what was this one?

fallen bobcat
#

forgot to pin the idle threads meme

#

actually that may have been it

#

CPU 0's idle thread gets stolen by CPU 1, runs on CPU 1, CPU 0 goes idle, it runs its idle thread -> both CPUs are running the same thread

heavy sandal
#

oh lol yeah thats a problem

fallen bobcat
#

ok again I think that was a bug but maybe not THE bug, but now I have another crash I think I can manage

#

some thread gets popped tiwce

#

aaaa I think I fixed it

#

this is wrong

#

the check should be before assigning td

#

no nvm that was a bug I think but still not the one 😭

heavy sandal
#

F

fallen bobcat
#

I will report back when I actually fix it

#

hopefully

heavy sandal
#

good luck

fallen bobcat
#

well now it doesn't crash anymore but idk why meme

fallen bobcat
#

I forgot to do it on amd64 too 🤦‍♂️

#

now I think it all works

#

turnstiles seem to work too letsgo

heavy sandal
#

🎉

fallen bobcat
#

well with no PI though trl

#

because I am skill issued I wanna clean up how priorities work first

fallen bobcat
#

not doing much work on this because I have had the coolest idea ever

#

and trying to do that instead

fallen bobcat
wet kernel
#

bootsector llama2 when

#

just gotta reduce it to a third of the current size meme

fallen bobcat
#

Just the attention loop and stuff is too big

fallen bobcat
#

work should resume soon™

#

first step will be to rework priorities so they're easier to manage

fallen bobcat
west talon
#

They would just eat it the fuck up

fallen bobcat
#

yeah i posted it for the meme

#

I wanted to try HN too but apparently its already been posted by someone else and didnt really gain attention (probably wrong time of post or something)

#

and you cant repost links on HN

#

also im posting it literally to boost my github even though I hate doing so for that reason, I am completely honest about that, I do think it's a cool meme though

#

also some dude made a PR adding tests and stuff, but it's like bigger than the code itself so idk if I should merge it 😭

fallen bobcat
#

I'm thinking I will put them in their own priority range or something instead

#

which means I'll just need to copy a number instead of copying a bool + a number

fallen bobcat
#

kinda wanna setup a website for this even though it's very early stage...

#

procrastination 101

#

mostly cuz I found a cool logo

#

@vital summit

vital summit
#

wats this

fallen bobcat
#

it has zinnia on it

#

next to zag

vital summit
#

what is it

fallen bobcat
#

cool public domain illustrations I like to use

vital summit
#

lol

fallen bobcat
#

they look neat

#

something like this looks cool

#

they also have this one but I dont think it looks very good

fallen bobcat
#

i think the conventional commit thing is a bit stupid

#

tempted to switch to just subsystem: <changes> but sunk cost fallacy trl

languid canyon
#

whats your current style?

fallen bobcat
#

this

#

but I have decided it's stupid

#

so now it'll be the linux/go style

#

I think it does help with making commits atomic though like no fixes mixed with features

#

but I can do that on my own

languid canyon
#

hm thats interesting i suppose

fallen bobcat
#

to me it feels like you cant really put commits in a bunch of baskets like that

#

who knows if it's a "chore" or a "refactor" or something

#

idk

#

feels weird

west kite
#

why do commits when you can GOOGLE_APPLICATION_CREDENTIALS=/home/pitust/.config/restic-keys.json restic backup ~/src ~/misc/prototype ~/misc/design --exclude-if-present .nobackup --exclude-caches --exclude-file ~/src/.resticignore -H nixos -r gs:[bucket name]:/ trl

heavy sandal
#

lmao

fallen bobcat
#

@heavy sandal I have a local version of zonfig working with zigzag 👀

#

that means it'll work with only zig as the dependency

heavy sandal
#

niice

fallen bobcat
#

thoughts?

#

it's less colorful than the old UI but I cbf to port over all the colors

heavy sandal
#

lgtm

fallen bobcat
#

seems to link much quicker I think

#

not sure why it's still using llvm link though

heavy sandal
#

llvm is still the default for some optimize modes iirc

#

i dont remember the details tho

fallen bobcat
#

well, I pushed it

#

the code quality sucks imo

#

but it works

#

so

#

well it could certainly be worse

#

but it could probably be better

#

can you try it on windows real quick @heavy sandal

heavy sandal
#

i dont have the computer on atm but can test it tomorrow

fallen bobcat
#

great, thanks

#

i still depend on qemu

#

time to write zemu

heavy sandal
#

lol

languid canyon
languid canyon
languid canyon
heavy sandal
#

the dep for the tui here probably matters more here because ncurses doesnt exist for windows

languid canyon
#

all u need is some device mmio emulation on top of the framework

languid canyon
heavy sandal
#

i will say zig+qemu is the same level of manual external deps as i have

#

imo not really worth going much further unless you find the idea of making your own emulator interesting enough to make a project of it

languid canyon
#

yeah

#

im not even sure what to call it if you just rely on kvm

#

its not an emulator, not a hypervisor either

heavy sandal
languid canyon
#

yeah ig

#

frontend is sensible

heavy sandal
#

the main reasons to eliminate the other systool deps and stuff apart from qemu is xplat building. like without dimmer i couldnt build my disk image on windows at all because the usual mechanisms just dont exist there in the same way

#

but youll obviously have the programming language you use as an external manual dep and qemu is rather complex and has downloads available for all major platforms already that you can just easily grab - and qemu isnt mandatory for osdev anyway theres other vm options etc depending on context and taste

fallen bobcat
#

though a toy x86 emulator would be fun

fallen bobcat
languid canyon
#

an x86 emulator would be pain af

#

since its so complex

fallen bobcat
#

no nvm

fallen bobcat
#

kinda

languid canyon
#

type 2 would be KVM

fallen bobcat
#

I have an 8086 emulator but I didnt implement the PC devices

languid canyon
#

qemu is just an app invoking the framework apis

languid canyon
heavy sandal
#

wait what are type 1/2 etc? never heard of that terminology before

fallen bobcat
#

well the CPU part isnt even that hard

fallen bobcat
languid canyon
fallen bobcat
#

like proxmox

#

I guess

#

?

languid canyon
#

Xen for example

fallen bobcat
#

vmware has bootable stuff too

languid canyon
#
Xen

Xen (pronounced ) is a free and open-source type-1 hypervisor, providing services that allow multiple computer operating systems to execute on the same computer hardware concurrently. It was
originally developed by the University of Cambridge Computer Laboratory and is now being developed by the Linux Foundation with support from Intel, Citrix, ...

fallen bobcat
#

i forget all the names

languid canyon
#

yeah vmware has one

#

type 2 is a hypervisor on top of a normal kernel

#

like KVM

fallen bobcat
#

qemu is type 3

heavy sandal
languid canyon
#

yeah

fallen bobcat
#

idk if whpx is one

#

maybe type 2 is hyper-v and whpx is type 1

heavy sandal
#

the secure/nonsecure kernel memes is fun, i kinda want to try that someday

heavy sandal
fallen bobcat
#

omg the bun rewrite thing

#

everyone has been talking about it for like 2 weeks straight it feels like

heavy sandal
#

oh god what now

fallen bobcat
#

idk

#

posts still being made

heavy sandal
#

im just ignoring all this shit now lol

fallen bobcat
#

yeah me too

#

my personal opinion of bun did lower quite a bit

#

I thought it was good

#

its not even about rust or zig

#

but it's just like straight up 100% vibecoded slop now

heavy sandal
#

ive had the amount of irl shit ive had to do like double 10 days ago and thats after it was already higher than normal a few weeks before that i dont have time to care about js frameworks

fallen bobcat
#

idk how you can feel good about that

#

it's a JS runtime 👆 🤓

#

I should write bun but not slop

#

call it nub

#

jk I dont have enough interest in the JS ecosystem to do that

heavy sandal
#

last time i cared about javascript, jquery was new and im happy to keep it that way

fallen bobcat
#

JS runtime is taking a JS engine like V8 or JSC (in the case of bun) and adding a bunch of stuff to interact with the OS

#

essentially

heavy sandal
#

yeah i know i just dont actually care about javascript so i cant be assed to use the right terms

fallen bobcat
#

bun also does package management and stuff

heavy sandal
#

ive had to write javascript recently, i just cant be assed to care about it anymore lmao

fallen bobcat
#

but anyway idrc about people saying "no one will use zig now... its dead forever"

#

so a popular project that was already kinda slop, completely slop rewrote in rust because it's easier to vibecode... sounds like a compelling argument for zig not against it lol

heavy sandal
#

yeah really

fallen bobcat
#

i made a ziggit post

#

because that might actually be something kinda useful

#

compared to most of my projects lol

heavy sandal
#

nice

fallen bobcat
#

now I want to make an x86 emulator

#

ffs

heavy sandal
#

recommendation, start with an assembler so you can get a hang of instruction encoding

fallen bobcat
#

it's the end of the term and I have a motivation burst

#

while getting fucked by exams

fallen bobcat
heavy sandal
#

ok so then youre ahead of the curve there

fallen bobcat
#

I wanted to write an IBM PC emulator at some point

#

but I couldnt get the devices to work like I wanted (wanted to get the ibm bios running)

#

so I gave up

#

but the 8086 worked fine

heavy sandal
#

i was briefly working on an emulator for a logic-gate-level breadboard cpu, with ui to be int he style of the edsac emulator, but got stuck on the ui of it and then found out that you can just write an os for the actual computers i have already and dropped that project

#

emulators are kinda fun to write tbh

fallen bobcat
#

I will get the vibecoding job offers thing and zag commits +1000% trust

fallen bobcat
#

Timing is a bitch

heavy sandal
fallen bobcat
#

I've written a fairly accurate gameboy emulator

#

And that's pretty much it

#

I did do a bunch of cpu emulators but the devices around them are usually annoying

#

Because of timing and stuff

heavy sandal
#

yeah

#

timing, hell for emulators and also for hardware work

fallen bobcat
#

honestly tho ive been thinking about it and I'm reluctant to start a new zig project

#

cuz the language is so unstable, I'll have to maintain more code

#

one project like zag is fine

#

but if you add like another 10kloc repo

#

ehhh

languid canyon
#

isnt your kernel zig lol

fallen bobcat
#

yeah

#

but like

#

I mean if i start making an emulator

#

then I have the config tool too

#

then i make another repo for something

#

i have to maintain like 5 different repos to new zig versions

#

vs just one or two

languid canyon
#

ah

fallen bobcat
#

especially userspace programs

#

because the stdlib is much more prone to change

#

also I think I have a cool name for the system as a whole

#

maze

#

and the kernel would be zag

#

since a maze is full of zig zags

heavy sandal
#

nice

languid canyon
#

lol

fallen bobcat
#

idk maybe i'll change my mind

#

still have to do actual work first

fallen bobcat
#

It can be unsettling when you notice that a technical assumption you've been making for 40 years has quietly expired. This happened to me a few minutes ago.

I maintain a game called "greed". It's an old-style game from the days of character-cell terminals. Not quite a classic

#

ESR too

#

wtf

long pendant
#

first, because the terms are just numbers with no description, which for some reason was irresistable to academics in the 20th century (i dislike the statistical term type I and II error for the sam reason)

#

second, because i don't think it's especially meaningful, if kvm is a type II hypervisor then probably vmware esx should be too, since esx is also a kernel of its own providing non-hypervisor services. the difference, as far as i know, is just that esx is more explicitly designed for hypervisor use. but this is such a continuous spectrum that i think trying to collapse it into a binary of I or II is silly

fallen bobcat
#

I should really finish the priority inheritance stuff

#

I havent had much motivation but hopefully once I get through that I can work on funnier stuff

#

claude add priority inheritance make no mistake

fallen bobcat
#

I think that is a good use of AI without polluting the code

digital bronze
round tangle
#

The book on the Solaris kernel describes this scheme in detail.

fallen bobcat
#

(I have implemented turnstiles, albeit without priority inheritance)

long pendant
fallen bobcat
#

yeah

fallen bobcat
#

@heavy sandal do you think andrewrk will kill me if i rewrite SmpAllocator and make it like 2k lines

#

ok yes I will work on this

#

the current SmpAllocator could be greatly improved

heavy sandal
#

lmao

#

if your improvement ends up good it might get accepted

fallen bobcat
#

super basic but the fast path is really fast

#

I think the current allocator's philosophy was "good bang for the buck"

#

since it's so simple it's fast

heavy sandal
#

yeah

#

thats very much what it was

#

a simple and maintainable but still fast enough option

fallen bobcat
fallen bobcat
heavy sandal
fallen bobcat
#

also

#

yeah the zigzag code does seem to have AI stuff in it

heavy sandal
#

riiiip

fallen bobcat
#

especially the // emdash emdash emdash comments

#

I did make a PR tho

#

I truly am a saint

languid canyon
fallen bobcat
#

(a moderator added it)

fallen bobcat
#

@rocky yoke can you tldr sharded_slab? I know it's basically mimalloc but I'm not sure what the chunk state stuff is

rocky yoke
#

do you have a specific question?

fallen bobcat
#

a few

rocky yoke
#

i haven't checked the mimalloc internals in detail. i mostly took the main idea and implemented the rest around it

#

so it's likely that the state tracking is a bit different

fallen bobcat
#

why didnt you use the segment stuff from mimalloc, that is you have one large 4 mib allocation and split it up in 64 kib pages, it seems you picked 256 kib?

#

actually I think I will steal mimalloc and combine it with a few simplifications from sharded slab

#

should be good enough

#

the mimalloc paper is pretty good but it doesnt go that much in detail

rocky yoke
#

ah, i just don't have that because i didn't implement it yet

#

that's on my TODO list for the slab

fallen bobcat
#

afaik it's to minimize syscalls?

rocky yoke
#

yeah, it helps to prevent churn in some cases, for example when repeatedly allocating large objects

fallen bobcat
#

also how is the atomic stack not prone to ABA? isnt it basically a treiber stack

rocky yoke
#

because we never pop from it

#

we only splice the whole thing

fallen bobcat
#

ahhh yeah

rocky yoke
#

compared to sharded_slab?

fallen bobcat
#

I'm not sure, just in general it'll be closer to mimalloc I think

#

I'm just tryna figure out the layout atm

#

like I'm not sure what's the difference between pages_direct and pages

#

why the fuck did they name slabs pages 😭

rocky yoke
#

i guess a page is not necessarily a slab, it can also be a large object

fallen bobcat
#

actually no

#

the slab equivalent would be the segment I think

#

I guess the pages would still be pages in bonwick parlance

#

idk

rocky yoke
#

no, i think a segment can have slabs of multiple size classes

#

at least, that's what i'd assume

fallen bobcat
#

I don't think so?

#

look at the diagram

#

you have one segment per class size

#

from which you allocate pages

fallen bobcat
rocky yoke
#

no, i think you have one page per size class

fallen bobcat
#

"it took me a moment to remember" tf are you talking about claude 😭

rocky yoke
#

the point of the segments is that you don't have to call mmap once per size class

fallen bobcat
#

ah yeah actually I think I was mistaken

#

the pages are the same size no matter what

rocky yoke
#

yes, the pages are the same size but they can be subdivided into objects differently

fallen bobcat
#

so it doesnt make sense to have one segment per size class

#

apart from large object sizes, which have less pages

#

it's a slab in the sense that you allocate fixed-size objects from it

#

but then you allocate fixed-size objects from those objects

languid canyon
fallen bobcat
#

yeah

languid canyon
#

Oh

fallen bobcat
#

I'm not 100% sure how mimalloc does it, but I'll do like sharded_slab and have one threadlocal heap

#

and use pointers

#

instead of indexing with id

#

I think the naming will go Chunk -> Slab -> Block

#

chunk = segment, slab = page

fallen bobcat
#

man the mimalloc code has like

#

10000 layers of abstraction

#

😭

#

worse than linux

fallen bobcat
#

omg

#

genius idea

#

mimalloc does this

#

I can just use a bitmap instead

#

actually an extremely good idea

#

idk why they didnt think of this

languid canyon
fallen bobcat
#

wdym

languid canyon
#

Why does it assert false

fallen bobcat
#

this is called when a segment is in the "segment_has_free_pages" queue essentially

#

huh

#

you're right

#

wtf

languid canyon
#

Indeed

fallen bobcat
#

no nvm this is normal i cant read

languid canyon
#

Cant u just have a freelist of unused segments instead of iterating them

fallen bobcat
#

a freelist of pages yea

#

idk why they didnt do that

#

or a bitmap

languid canyon
#

Maybe thats not hot code

fallen bobcat
#

the bitmap idea is really good because it has other nice properties

languid canyon
#

Otherwise this is quite bad

fallen bobcat
#

like you can just do if (bitmap == 0) instead of used == capacity

#

there is probably a phd reason why they didn't use a freelist

#

idk what

rocky yoke
fallen bobcat
#

it being a slow path != "we must make it as slow as possible"

rocky yoke
#

well, at most this iterates over 64 bit entries

rocky yoke
#

it's true that a bitmap would still be better even in that case though

fallen bobcat
#

idk for large allocs they have one page spanning the whole segment

#

also I think what I'll do is <= 8 kib is small, <= 512kb is large then I'll just hand it off to the OS

rocky yoke
fallen bobcat
#

it'll be the whole size of the segment

rocky yoke
#

yeah but that wastes a lot of memory (?)

#

or how do they do it

#

surely they don't round up everything over 64K to 4M?

fallen bobcat
#

no, it would be 512 kib

crimson sand
rocky yoke
#

i'd use a bitmap to allocate multiple 64K pages at a time

fallen bobcat
rocky yoke
#

for 64K <= size < 2M or so

#

and above 2M i'd hand to the OS

crimson sand
fallen bobcat
#

it's from v1

rocky yoke
fallen bobcat
#

actually wouldnt it make more sense to make the segment 2mb

#

since large (OS) pages exist

rocky yoke
#

well, 4M will just be 2 large pages

fallen bobcat
rocky yoke
#

Hm?

fallen bobcat
#

well, if the object size is like 128kb

#

and pages/slabs are still 64kb

#

then you need to allocate 2 to fulfill the request right?

rocky yoke
#

yeah

fallen bobcat
#

that gets tricky

#

I think maybe it makes more sense to make the page size be the size of the object

#

idk

#

I agree it's weird that they waste a bunch of memory for objects that are like 512kb

#

maybe I misunderstood

fallen bobcat
rocky yoke
#

with a simple freelist?

fallen bobcat
#

so instead of having multiple 64kib pages you have one large page of 4 mib from which you allocate objects

#

yeah it's the same as a regular page

#

except there is just one

rocky yoke
#

but this one page is still split into equally sized objects?

fallen bobcat
#

yes

#

I think this is to ensure at least 8 objects fit in a page or something like that

rocky yoke
#

i.e., you'll have a 512K page consisting of 4 objects of size 128K or similar

fallen bobcat
#

yes, well a 4 mib page

rocky yoke
#

ah

fallen bobcat
#

since a segment is 4mib

fallen bobcat
rocky yoke
#

yeah i see

#

ah i guess for multi page allocation with a bitmap you wouldn't know which segment to look at

#

but this means that you're potentially wasting a lot of memory still

fallen bobcat
#

how?

#

the most you're wasting is through internal fragmentation

#

huh actually you do also waste a whole object since you have to embed the metadata

rocky yoke
#

because there are 7 such size classes and for each one you're potentially wasting 4M (- epsilon) per cpu

#

so you could easily be wasting a gigabyte on a machine with 20 cores

#

actually, half a gigabyte but still

fallen bobcat
#

idk how you're wasting 4M any more than if you were allocating small objects

rocky yoke
#

because you can fit pages of different size classes into the same segment

fallen bobcat
#

if obj_size < 8kib, carve up 4mib segment into a bunch of pages, carve pages into objects
if obj_size < 512kib and > 8kib, carve up 4mib segment into a single page, carve page into objects

rocky yoke
#

for small objects

#

but you can't do that for large objects

fallen bobcat
#

ah right

#

what could be better you think

#

this is tricky

languid canyon
fallen bobcat
#

because the paper describes v1

#

and it's considerably simpler

#

And hasn't changed that much

languid canyon
#

Maybe they improved critical stuff you won't see

#

Is this for the kernel allocator?

fallen bobcat
#

And they fix bugs on older versions anyway

fallen bobcat
heavy sandal
#

because what should one do upon seeing suboptimal std allocator code but redo all the things to maybe PR in

fallen bobcat
#

It's not suboptimal

#

It's pretty good for what it is

#

But on cross thread frees specifically it has like O(infinite) memory usage lol

heavy sandal
#

lol

fallen bobcat
rocky yoke
#

What does it do for cross thread frees?

fallen bobcat
#

It doesn't

heavy sandal
fallen bobcat
#

yeah I was about to send

#

Andrew claims it is faster than glibc but I kinda doubt that

#

It's faster on his benchmarks sure

rocky yoke
#

Faster than glibc doesn't mean much unless it's a broad benchmark

heavy sandal
rocky yoke
#

You can always make benchmarks where you're good at see the sharded_slab ones

fallen bobcat
rocky yoke
#

Does it ever return the memory?

#

If not, it'd oom on producer/consumer style code

heavy sandal
#

if its a large object yeah

fallen bobcat
#

Specifically

rocky yoke
#

where one thread allocs and another one frees

fallen bobcat
#

yes

#

In that scenario it'll OOM

#

it's a known issue

heavy sandal
#

it actually tries to steal from other threads fwiw

#

but yeah

#

(each thread gets a lock, the stealing uses trylock instead of unconditional)

fallen bobcat
#

Also

#

It may be faster

#

But it's probably not better

heavy sandal
#

yeah

fallen bobcat
#

lots of internal fragmentation

#

with just power of two

heavy sandal
#

at least zig makes it super easy to swap out your own allocator if you want, this one is just meant to be a good-enough default for releasefast builds

#

so even if your improvements dont get merged you could make a package of it

fallen bobcat
#

Like Linux

fallen bobcat
#

something like 1 mib

#

so worst case drops from 7 * 4 mib to like 7 * 1 mib per cpu

#

I'll do the mimalloc thing for now, I'm not sure how this should be handled

#

I could also have some kind of "run" metadata which encodes a number of 64kib pages

rocky yoke
#

that's ofc a bit slower than a freelist but it could still be thread local

#

So it wouldn't be that bad

fallen bobcat
#

ugh the asymmetric workload performs a bit worse

#

I'm not sure why

fallen bobcat
#

@heavy sandal

#

running andrew's own benchmarks

#

lol

#

the only worse thing is RSS but that's because i havent implemented returning pages to the OS yet

#

also gotta love the asymmetric benchmark using infinite memory on SMP

heavy sandal
fallen bobcat
#

imagine a production database like tigerbeetle or something that's running for a while

#

it will OOM

fallen bobcat
#

@heavy sandal im spamming you but do you know where zig devs are

#

IRC or ziggit

#

ah they have a compiler development channel on zulip, but I dont think this is what I'm looking for

#

nvm it is, sorry for ping

#

they have a ZSF zulip

#

oh yeah this is great

#

I think I can ask andrew and people directly

heavy sandal
#

yeah it's zulip

#

there's a zig dev zulip and a zig community zulip, the former is probably what you want and has an std channel

fallen bobcat
heavy sandal
fallen bobcat
#

probably better if I just share the file for now

#

also

#

remember the asymmetric workload

#

for some reason it's much faster on my ARM macbook

#

on my amd64 machine smpallocator and my own were pretty much neck-and-neck, but on my macbook mine is much faster

languid canyon
fallen bobcat
languid canyon
#

Ah

fallen bobcat
#

also i could maybe potentially reuse the code for my OS

#

we'll see

#

after all, why not have a 4th allocator in the kernel trl

languid canyon
#

a kernel is basically a collection of allocators soo

fallen bobcat
#

yeah but memory allocators in particular

#

i have 3

languid canyon
#

wait why

fallen bobcat
languid canyon
#

whats zone

fallen bobcat
#

my slab allocator

#

pretty good

languid canyon
#

ah

fallen bobcat
#

technically even then, the slab allocator is a bunch of small allocators

#

a bitmap and a freelist

past dome
#

if you rotate the b 180 degrees then its a slaq allocator

fallen bobcat
#

lol is this AI

languid canyon
#

Yes

pastel citrus
#

yeah

rocky yoke
#

I see that Zig takes it's AI policy very seriously

brave tangle
#

*its

pastel citrus
#

it's not zig where that review was made

#

and it's not a zig contributor either or anything

#

(afaik)

heavy sandal
#

yeah

#

the zig no-ai policy is for zig itself. other projects are welcome to do what they want

fallen bobcat
#

I think im ready to post my thing on zulip for RFC at least

#

I've implemented alignment

fallen bobcat
#

wait do u think I will get slimed since I said I used an LLM to generate preliminary tests/benchmarks

fallen bobcat
# stray kelp no

i wasnt aware you were experienced with zig's contribution policy :p

stray kelp
#

tho whatever you do on your project should not really matter i think

fallen bobcat
#

I'm aware

#

I'm trying to contribute to zig

stray kelp
#

ahhh

#

i tought you meant your project

fallen bobcat
#

I've had a stroke of genius while in the shower wrt returning pages to the OS

#

i'll call them superslabs or chunks here but they're mimalloc segments

#

basically everytime a superslab's refcnt drops to 0 (i.e all of its slabs are freed), you put it in a thread-local list of "free candidates", if refcnt ever goes back to > 1, you remove it from that list (to prevent always putting and removing it from the list in the case where 1 object is constantly freed and allocated). Then, on every heartbeat/slow path of the allocator, you go through every superslab in the list and increment its age. When its age reaches a certain threshold, you move the superslab to a global superslab freelist and reset its age. Then, on a slow path, you also go through the global superslab freelist once in a while, increase the age of every superslab and then when it reaches a threshold you free it (or madvise it)

#

this ensures that superslabs get the chance to be reused before getting returned to the OS by 1. you 2. other threads

#

i think this is pretty good and wouldnt surprise me if the guys at MS figured out something similar

#

cba to read mimalloc code though

fallen bobcat
#

man i just saw a hello world zig OS essentially with 100 stars 😭

#

really need to lock in so I can claim zig OS supremacy trl

vital summit
#

post on hn without any code

fallen bobcat
#

new random paper dump

fallen bobcat
#

lmfao what

granite kiln
#

what the fuck lol

vital summit
#

huh

digital bronze
fallen bobcat
digital bronze
#

Ok, no, that was Gemini

fallen bobcat
#

but like this is conspiracy theory shit lol

fallen bobcat
#

"the zig developers have been putting secret code in the compiler ever since the first release to detect AI usage"

fallen bobcat
#
#

i swear ziggit will have the lowest quality posts 😭

fallen bobcat
#

i have committed unspeakable crimes against the standard committee

languid canyon
#

whats a pin

fallen bobcat
#

mhm I guess the name should be publish_epoch

fallen bobcat
#

I will get back to work on zag tho since I consider this mostly ""finished"" (as a first v1) for now

heavy sandal
fallen bobcat
#

I think it's pretty cool but it will be extremely cool if I add channels

#

But that's a whole can of worms that I kinda don't want to open now

#

I'll wait to see community feedback and see if it's worth pursuing

#

Also I should really try getting my allocator in

heavy sandal
#

are the queues in it lockless?

#

because a lockless mpmc is something ive been wanting

fallen bobcat
#

Yeah it's all lockless

digital bronze
fallen bobcat
#

Idk if you've ever used crossbeam, but like that

digital bronze
#

But in kernel?

fallen bobcat
digital bronze
#

Oh, I'm dumb

fallen bobcat
#

@past dome turnstile man how do u test turnstiles/priority inheritance

fallen bobcat
#

when i get to a somewhat workable state I kinda wanna port to a weird architecture

#

LA64 would be a good first port tho before that i think

heavy sandal
#

la64 seems kinda neat tbh

#

apparently it has dedicated registers for setting up an hhdm in the normally-non-canonical zone of virtmem

digital bronze
#

Its lapic equivalent is a bit weird though spun

long pendant
#

i don't know anything about it yet so i'll take your word for it

digital bronze
#

So it's like a mixture between plic and lapic

#

Idk, it seemed a bit odd to me

long pendant
#

whence 'local'

digital bronze
#

I.e. in a system with 2 processors with 4 cores each, you would have 2 controllers

#

Where each of 4 cores share the interrupt vectors

#

On x86 it's per hardware thread

fallen bobcat
#

I don't think having a heap for the turnstiles is worth it

#

it seems pretty tricky to have to deal with the keys updating under the hood, i.e raises need to remove the waiter from the heap and re-insert it, and when a priority gets lowered then there's a whole can of worms here which would require scheduler integration

#

I suppose the priority getting lowered isnt that bad and would be essentially benign (it would only over boost) but I dont like it

#

maybe a rbtree would be better here if removes are frequent but I'm just gonna keep a list

fallen bobcat
#

work will continue tomorrow 🚀

#

since will has a j*b that means this is an opportunity to steal mintia's ideas and implementing them first so I can claim I came up with them trl

vital summit
past dome
#

just bc I got the jab doesn't mean I'm going to die

#

antivaxxer

wet kernel
#

No real osdev has a j*b

fallen bobcat
#

i have actually locked in and turnstiles are pretty much done i think

#

i havent been able to focus for more than like 5 mins these last few days

#

i need the autistic motivation to come back

heavy sandal
#

mood

vital summit
#

now you get to enjoy autistic demotivation

fallen bobcat
#

finally pushed 🚀 🚀

fallen bobcat
#

ok great now worker thread time

#

should I go full fancy mode and do the chase-lev lock-free workstealing queue

#

actually dunno if that's a good idea since work items can block

fallen bobcat
#

actually i think I will work on an object manager of some sort, I like that idea

earnest heart
#

interesting language

#

it looks like propagate can live lock when two pi walks hold different root turnstiel chain locks and each needs the other's bucket?

#

because restart doesn't drop root

fallen bobcat
#

ooh I think you're right

#

good catch

#

I think I can enforce an ordering on the chain locks using the address

#

actually no that gets iffy

#

i should just drop the root and restart entirely

#

I think I fixed it, thanks @earnest heart

fallen bobcat
heavy sandal
#

ill say straight up the only reason to go full NT with object manager is if you dont have a singly-rooted vfs

#

ime

fallen bobcat
#

i want a proper unix vfs tho

heavy sandal
#

yeah

#

exactly

fallen bobcat
#

object manager will just be a way to manage reference counts

#

probably wont even be its own subsystem

heavy sandal
fallen bobcat
#

cuz i wanna get worker threads ASAP, and threads will be objects

#

or maybe I can just skip that for now trl

#

yeah ok

#

I really need to start writing stuff down, I had a bunch of cool ideas I forgot

heavy sandal
#

mood

#

one thing I recently realized tbh is that the full nt object manager stuff is actually basically just devfs + a tmpfs but only for pipes + the /mnt directory

fallen bobcat
#

I will expose objects through the VFS

#

but since the main goal is POSIX/linux compat they wont be used that much outside of the kernel

#

userspace probably cant get a handle to anything but files (or well, also stuff like timers through timerfd etc)

heavy sandal
fallen bobcat
#

linux workqueue is pretty complex

heavy sandal
#

among the least surprising sentences ive ever read

fallen bobcat
#

I think I have an idea of a fairly good/simple design

#

There are 3 global work item queues: high, normal and low.
The high and normal both have a thread each on every CPU and for the low-priority workqueue there is one global thread.
When a thread is spawned, it registers a "on block" hook so that the workqueue can detect when it blocks and spawn another thread accordingly, this new thread is then killed off if it cant find work for something like 200ms.

fallen bobcat
#

mhm

#

is it really sensible to have one thread per priority per cpu

#

feel like that may use a lot of stack for threads that'll be idling most of the time

#

for now it's fine I guess trl

fallen bobcat
#

whats a good name for a queue that blocks when it's empty

#

I realized i use that in workqueues and I'll probably need it for the reaper thread, so might as well make it generic

heavy sandal
#

and tbh i cant think of a better name than blocking_queue lol

fallen bobcat
#

no way I'm calling it that

#

yeah blocking_queue isn't bad

#

or event_queue

heavy sandal
#

(the c# one is called blocking collection instead of blocking queue because its technically a wrapper to add blocking to an arbitrary concurrent collection)

fallen bobcat
#

huh, it looks like NT has a queue object structure

#

that is a blocking queue but it also wakes threads when one of them blocks

heavy sandal
fallen bobcat
#

I have thought of a way to have that and have a worker pool on top

long pendant
#

so you provide it with say 20 threads and want 10 around, and it lets 10 through, and if one blocks while servicing a request (i.e. blocks on anything but the event queue), it releases another, or something like that

past dome
#

Mintanium implements this

#

I mean itantia

#

I mean mintia

#

I called it KeBalancedQueue

crimson sand
past dome
#

Yes

nimble seal
#

With the caveat that work queues use priority queues, which are a new type of dispatcher object relative to the 90s

fallen bobcat
#

workqueue will use a kernel queue but will hook into it to manage spawning threads dynamically

wet kernel
fallen bobcat
#

ok it will be a fake queue

#

it seems NT does direct handoff of items when they're added to a waiter thread

#

I wont do that

#

I'll just add the item to the queue and wake one waiter and hope it picks it up

#

if it doesnt then it'll just go back to eep

fallen bobcat
#

which is kinda crazy

past dome
fallen bobcat
#

yeah I know

#

but it's fun

#

do you do that?

#

I think the worst case here is that thread gets woken up for no reason

#

which I dont think is that bad but could definitely be improved

past dome
#

I don't think I do because I need the wait status

fallen bobcat
#

yeah but handoff of new items to woken threads in general

past dome
#

No

#

It just wakes one up and lets it go through again in a loop and find it

#

It's bad to give it to a thread that might not get scheduled in for a while

fallen bobcat
#

wonder why NT does this then

earnest heart
#

well, if you merely enqueue and wake, you now have a race where the item is visible to everybody before the selected waiter actually runs

fallen bobcat
#

yeah but that is not a problem, since the waiter will just loop and find that the list is empty and go back to sleep

past dome
#

modern NT?

fallen bobcat
#

idk im basing this off older NT

past dome
#

It's a bad idea for the same reason that atomic lock handoff (done 100% of the time) is a bad idea

#

By enqueuing it instead of handing it off, someone who is already running on another cpu can come through and find the work item and start working on it immediately by the time the woken thread gets scheduled in

fallen bobcat
#

yeah that is my reasoning too

#

maybe it just has to do with the properties of the queue object they have to maintain

#

or maybe they figured out that was bad and removed it in modern nt

past dome
#

I wanted to make my worker threads wait with a continuation so their stacks get released but it's a bad idea

#

It could increase latency intolerably at random moments

#

It's maybe a good idea for the low priority ones

fallen bobcat
#

I'm not convinced one thread per priority per cpu is right yet but on 64-bit systems the memory usage is minimal

past dome
#

It should be more than one

#

Even mintia2 does more than one

fallen bobcat
#

my plan is to do one but if it blocks it starts another

past dome
#

per priority per cpu

fallen bobcat
#

this is what linux does

past dome
#

do you mean allocating and initializing a whole other thread right then and there?

fallen bobcat
#

basically there'd be some kind of pool manager thread that would get woken up when one of the workers block on something else than the queue, then it would spawn another thread to handle the work. Then that thread waits for new work or for 300ms and if it times out then it exits

#

if it can't spawn a thread directly (because no memory or something) there's a rescue thread that drains the workqueue to hopefully release memory

#

that is basically what linux does afaiu

past dome
#

I would keep a constant pool of some minimum number of worker threads and have a periodic thread check whether satisfactory progress is being made and if not it spawns more worker threads

#

Also I have per node queues rather than per cpu and there's N worker threads per cpu in the node

fallen bobcat
#

any particular reason why the queues are global like that? to avoid weird imbalances?

#

with global queues im worried about contention

past dome
#

I'm not anticipating that much intra-node contention at some given priority level of the worker queues

#

A lot of the work items are batched for the future

#

For example the thread reaper work item is enqueued for like 500ms in the future and any threads that die in that timespan won't enqueue it again

#

Same with object reaper (used to avoid boundless stack usage in object deletion and also some deadlocks)

fallen bobcat
#

huh you do reaping at a periodic interval?

past dome
#

No

#

When a thread dies and the local node's reaper queue is empty, it will enqueue the reaper work item

#

For 500ms in the future

#

It's a timed item

#

That's not periodic that's just delayed and batched

fallen bobcat
#

didnt vms do that or something

past dome
#

Idk

fallen bobcat
#

ah no nvm

#

I remember reading this

past dome
#

Oh yeah

#

One thing I was missing is that the swapper process did wake periodically to do some odd jobs

#

Regardless of whether memory was low

#

So it'd be like once every 5 sec

fallen bobcat
#

I'm not sure what NT's current count means, but mine will mean "number of threads that are ready to process work"

#

for work queues that maximum count will be one, but in practice there might be more than one thread running

#

i think

fallen bobcat
#

it's a bunch of extra logic for something i wont really use

heavy sandal
fallen bobcat
#

yeah well I wnated to do both

#

basically any thread waiting on remove() and any thread currently doing work

#

but i think it adds to much complexity for too little gain

heavy sandal
#

shrug

fallen bobcat
#

casual 8k lines file

#

i somehow have found an unlimited chatgpt plus hack

#

i used the free trial, then in paypal app I removed openAI from it

#

and now I still have plus even though the month has ended

#

codex is pretty useful going thru linux code

languid canyon
fallen bobcat
#

tbf there are a lot of comments

#

ok i figured out how it works basically

#

and it's better than what I wanted to do

languid canyon
fallen bobcat
#

the pool always tries to keep at least one running worker.
When a worker runs, it checks to see if there are enough idle workers, if there isnt then it creates one.
When a worker goes to sleep, the pool tries to wake up an idle worker.

#

then when a worker wakes back up, it checks if there are too many workers and if so it goes to sleep

#

that is basically it

languid canyon
#

interesting

fallen bobcat
#

mhm designing this properly is hard

#

the way my queue works doesnt really work with that model

fallen bobcat
#

bruh the queue code makes the wait code so messy lol

fallen bobcat
#

ok so I have an actual proper NT style queue now

#

i think for worker pools it'll be roughly "keep always one idle spare thread" per-priority per-cpu

#

so that's like two worker threads per priority per cpu minimum

#

but it can grow as there is more pressure on the queue, there'll be some kind of periodic pruning of threads that havent done anything but waiting in the last e.g 1 second

#

per-cpu queues have maximum concurrency of 1, and the global low-priority queue will have one of ncpus

#

to detect thread creation blocking because of no memory, you set a timer that fires in like 20ms, and if the worker is still not created you enqueue the rescuer thread which drains the work items required to reclaim memory

#

dunno if that should be a thing or if thread creation should return an error instead if OOM

#

Oh @past dome just thought about something, if you delay thread reaping then what happens if you OOM before the reaping happens? Do you expedite reaping in that case?

past dome
#

gibbee

fallen bobcat
#

I guess you could just cancel the timeout or something dunno

#

Or just enqueue it directly

past dome
#

if memory is very very low then the balance manager thread (which is awoken by low memory event) will cause the reaper to be called immediately

#

while emptying other caches

fallen bobcat
#

Ah yea

fallen bobcat
past dome
#

does this not suffice.

fallen bobcat
#

You don't have to deal with that problem cuz you don't spawn worker threads dynamically

past dome
#

but i will