My Struggles with puffs_cc

2026-11-09

Who wants to read about my failures? Maybe someone will read this one day and be able to actually help! Maybe I will look back and reminisce on all the pain and all the things I've learned with warm fuzzies. In 2026 it is just pain, and I think I am pretty close, I just need one more lightbulb to flicker somewhere in my brain.

The End

I have failed to setup puffs_cc in a way that practically improves the performance of my filesystem.

The Beginning

If you have been looking over the performance metrics you likely saw some really nice improvements to reads on the puffboy. Particularly for single read operations, the puffboy is operating with a pretty minimal lag. Problems arise as the requests pile up because every single request is enqueued and performed sequentially. Even if there are 100 requests for the same file, each read operation is separate and must wait for the previous ones to complete.

And I know that caching could help solve this problem. But where is the fun in that? Caching is just hiding the performance problems for convenience, the purpose of this project is to say the web pages came from a gameboy and caching them elsewhere seems like cheating.

Thankfully NetBSD includes puffs_cc(3) and puffs_framebuf(3) as a means to event handling for networked file systems, and the JOYBUS protocol at least has a similar latency to a network call so this seems like an idea worth exploring. Pretty much puffs_cc allows you to tell the puffs main loop that you are waiting for something async so it can handle other requests via puffs_cc_yield(). Then you can tell the main loop you are ready with puffs_cc_schedule() to resume where you left off. At least that is my understanding. Now the puffs_framebuff docs are pretty intense for me, but it seems to be something that can be leveraged when working with sockets, which I am not, so I am only focusing on my own puffs_cc for now.

The Idea - Pooling the Reads

Given that this blog is small I am working with the assumption that if there is a spike in traffic that it will all be directed towards the same resources. Going back to the original problem, if there are 100 requests to this page, I dont want the 100th dude to have to wait for the other 99 to get the same thing that he is waiting for anyways. Wouldnt it be nice if the 100th dude could piggyback off of someone else's request to the same resource?

The idea is to move all read operations to a separate thread with a shared queue. Any time a read call comes in, the main thread checks to see if that read request is already on the queue. If its already in the queue then it just has to subscribe to that pending request. If it is not, then it can add an entry to the queue and wait. The separate thread is constantly popping off the queue read operations to pass over the wire and upon completion can inform everyone waiting.

          +-----------------+          +---------------+                +---------------------+              
          |  worker thread  |          |  main thread  |                | puffboy_node_read() |              
          +-------+---------+          +-------+-------+                +---------+-----------+              
                  |                            |                                  |                          
                  |                            | read() -------------------------->                          
                  |                            |                                  |                          
                  |                            |                       +----------+-------------------------+
                  |                            |                       |if queue.has(request):              |
                  |                            |                       |  node = queue.get(request)         |
                  |                            |                       |  node.subscibers.push(pcc)         |
                  |                            |                       |else:                               |
                  |                            |                       |  queue.push(new node(request))     |
                  |                            |                       +------------------------------------+
                  |                            <------------------------puffs_cc_yield(pcc)                  
+--------------------------------------+       |     tell main thread             |                          
|loop:                                 |       |     to move on                   |                          
|  node = queue.pop()                  |       |                                  ... execution paused       
|  resp = do_joybus_read(node.request) |       |                                  |                          
|  for s in node.subscribers:          |       |                                  |                          
|     memcpy(s.resp, resp)             |       |                                  |                          
|     puffs_cc_schedule(s.pcc) -+      |       |                                  |                          
+-----------------+-------------|------+       |                                  |                          
                  |             +-------------->                                  |                          
		  |                            +---------------------------------->                          
                  |                            |             resume               |                          
                  |                            |                                  |                          
                  |                            |                                  |finish up read operation  
                  |                            |                                  |                          
                  |                            |                                  |                          
			

Seems like a decent idea no?

Test: Concurrent Reads to different files

I got things sort of working and its time for some tests:

for i in 1 2 3; do cat "mnt/test$i.txt" & done; wait
# OUTPUT:
1793654454693 - no match found. enqueuing hash: 440126477 / fileid: 6 / copylen: 512 / offset: 0
1793654454693 - no match found. enqueuing hash: -576736757 / fileid: 4 / copylen: 512 / offset: 0
1793654454712 - no match found. enqueuing hash: -68305140 / fileid: 5 / copylen: 512 / offset: 0
starting read for key 440126477
finished read for key 440126477
1793654456799 - no match found. enqueuing hash: 440258575 / fileid: 6 / copylen: 512 / offset: 512
			

From the logs you can see that we are interleaving the reads. It starts trying to read file 6 and yields back to try reading file 4 and so on. Instead of waiting for file 6 to completely finish it moves on which is exactly what we want!

Test: Concurrent Reads to the same file

Lets get to what we all came here for. How is this gonna work for the pooling of similar requests?

for i in 1 2 3; do cat "mnt/test.txt" & done; wait
# OUTPUT:
1793654539295 - no match found. enqueuing hash: -1085168374 / fileid: 3 / copylen: 512 / offset: 0
starting read for key -1085168374
finished read for key -1085168374
1793654541390 - no match found. enqueuing hash: -1085168374 / fileid: 3 / copylen: 512 / offset: 0
starting read for key -1085168374
...
			

That's not good. It is as if it only can enqueue one thing at a time, like something is locking the entire file so two processes cannot read it concurrently. Boo!

Diving into VFS Internals

According to vnode(9):

The vnode lock can operate as a multiple-reader (shared-access lock) or single-writer lock (exclusive access lock), however many current file system implementations were written assuming only single-writer locking.

So many questions - but the good news is the concept of a shared lock exists. How does the file system specify which type of lock it wants? Does PUFFS allow for a shared lock for read calls? Does puffs do its own file locking outside of the file system?

The NetBSD Wiki has a whole article explaining filesystem internals but things did not really come together until I started mucking around in the code. They offer a more detailed explanation than I can provide but I will share what I walked through slowly over many days. The NetBSD-src has a directory dedicated to the puffs filesystem so I started there and found something in the puffs_vnops.c

const struct vnodeopv_entry_desc puffs_vnodeop_entries[] = {
	{ &vop_default_desc, vn_default_error },
	{ &vop_parsepath_desc, genfs_parsepath },	/* parsepath */
	{ &vop_lookup_desc, puffs_vnop_lookup },	/* REAL lookup */
	// ...
        { &vop_lock_desc, genfs_lock },			/* REAL lock */
			

There is the vector that defines the puffs implementation of all the file system operations. We can see that the locking logic is handled by genfs_lock. There are several man pages and references that suggest these are generic/shared implementations for all the different filesystems. Looking at the implementation you will see something like this:

int
genfs_lock(void *v)
{
	struct vop_lock_args /* {
		struct vnode *a_vp;
		int a_flags;
	} */ *ap = v;
	vnode_t *vp = ap->a_vp;
	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
	int flags = ap->a_flags;

	// ...
	op = (ISSET(flags, LK_EXCLUSIVE) ? RW_WRITER : RW_READER);
	if (ISSET(flags, LK_NOWAIT)) {
		if (!rw_tryenter(&vip->vi_lock, op))
			return EBUSY;
	} else {
		rw_enter(&vip->vi_lock, op);
	}

	// ...
}
			

So looks like genfs does in fact support a shared lock. But how does the flags variable get set? It is dereferenced from the arg passed to genfs_lock(). So something even higher up is setting the flag... I first tried to grep around for LK_EXCLUSIVE and LK_SHARED. It had a bunch of results but it wasnt clear which methods kicked things off. I decided I wanted to get the full stack trace and the easiest way I could think was to cause a kernel panic in the puffs_vnop_read(). Something somewhere in the stack is setting that flag right? I added a KASSERT(false) and here is the stacktrace:

at vpanic+0x154
at kern_assert+0x60
at puffs_vnop_read+0x74
at VOP_READ+0x40
at vn_read+0x1e0
at dofileread+0x84
at syscall+0x110
			

This is where if you read the NetBSD Wiki things will start to click. The VOP_ calls somehow map to the filesystem's operation vector. The wiki and man pages say you should never call the things like VOP_READ directly and instead use the methods like vn_read. Here is a snippet from vn_read()

static int
vn_read(...)
{
	// ...
	if (offset == &fp->f_offset && (flags & FOF_UPDATE_OFFSET) != 0)
		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
	else
		vn_lock(vp, LK_SHARED | LK_RETRY);

}
			

That if block is the answer but looking at it makes no sense to me. What the heck is this offset stuff and how is it getting set? What is interesting is that dofileread() doesn't seem to actually call vn_read() so there is still some magic I am missing. But there are some insights to be had from dofileread() - grepping around shows that it is invoked in two different places!

There is a sys_read() and a sys_pread(). Turns out there are multiple system calls to read a file. And sys_pread seems to do stuff that maybe touches the fp->f_offset.

With some more internet digging I learned that cat uses sys_read(). I need a way to get a sys_pread() call to truly test the concurrency! And it just so happens (through some experimentation) that lighttpd and nginx use sys_pread() when reading the files!

It is still not working

I tried to use httperf to blast the server, hoping to see some sweet sweet performance gains to no avail. I updated the puffs operation vector with my own locking call to confirm that lighttpd/nginx do in fact use LK_SHARED, but the debug logs show that it still operates one at a time. Something else, whether it is in the webserver or the puffs looping logic seems to be preventing this from working how I want. Or maybe it is in httperf somehow I dont know... I am going to try and write a very basic program to thread some sys_pread() calls to see what happens.

Still more bad news

There is a big, big performance hit to the single sequential reads with my current puffs_cc implementation. As documented in their man page, puffs_cc_schedule() will schedule the pcc sometime in the future. With my code as it is, that sometime is roughly 70ms in the future. That blows, I need to find a way to wake the main thread up sooner. Without that I dont think any of this is worth it.