Skip to content

Instantly share code, notes, and snippets.

View bnoordhuis's full-sized avatar

Ben Noordhuis bnoordhuis

View GitHub Profile
@bnoordhuis
bnoordhuis / container_of.rs
Created January 26, 2014 21:41
container_of() in Rust. Look up address of embedding struct.
// Copyright (c) 2014, Ben Noordhuis <info@bnoordhuis.nl>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
@bnoordhuis
bnoordhuis / gccbug.c
Created May 19, 2013 23:42
gcc + asm + on-stack iovec = uninitialized memory
// Compile at -O0 and -O1 or higher and compare the output of
// `strace -e pwritev a.out`. At -O0:
//
// pwritev(1, [{"Hello ", 6}, {"world!", 6}, {"\n", 1}], 3, 0) = -1 ESPIPE
// (Illegal seek)
//
// At -O1 or higher:
//
// pwritev(1, [{"", 0}, {"H\211l$\330L\211d$\340H\215-\367\10 \0L\215%\350\10
// \0H\211\\$\320L\211l"..., 4195360}, {"", 0}], 3, 0) = -1 ESPIPE (Illegal
This file has been truncated, but you can view the full file.
[root@server ~]# strace -fp 5497
Process 5497 attached with 2 threads - interrupt to quit
[pid 5498] futex(0x7fa1b80008c8, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
[pid 5497] epoll_wait(5, {{EPOLLIN, {u32=10, u64=10}}}, 1024, 4294967295) = 1
[pid 5497] accept4(10, 0, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK) = 11
[pid 5497] futex(0x7fa1b80008c8, FUTEX_WAKE_PRIVATE, 1) = 1
[pid 5498] <... futex resumed> ) = 0
[pid 5498] nanosleep({0, 900000}, NULL) = 0
[pid 5498] futex(0x7fa1b80008c8, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
[pid 5497] futex(0x7fa1b80008c8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
@bnoordhuis
bnoordhuis / trace-jit-code-events.patch
Created January 25, 2013 23:07
Print real-time V8 JIT code events.
diff --git a/src/node.cc b/src/node.cc
index 32cff12..3c5b616 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -2954,6 +2954,36 @@ static char **copy_argv(int argc, char **argv) {
return argv_copy;
}
+
+static void OnJitCodeEvent(const JitCodeEvent* event) {
@bnoordhuis
bnoordhuis / context-1.cc
Created November 21, 2012 14:51
V8 context GC cleanup failure
#include "v8.h"
#include <assert.h>
#include <unistd.h>
using namespace v8;
struct WrappedContext
{
WrappedContext(Handle<Object> obj)
{
@bnoordhuis
bnoordhuis / 1.js
Created November 4, 2012 02:01
v8 big array performance bugs
for (var a = [], i = 0; i < 64*1024*1024; ++i) a.push(123.456);
@bnoordhuis
bnoordhuis / cpuid.c
Created September 13, 2012 16:21
print cpuid info
/*
* Copyright (c) 2012, Ben Noordhuis <info@bnoordhuis.nl>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
@bnoordhuis
bnoordhuis / echo.js
Created September 11, 2012 22:46
simple net + child process echo server
var child_process = require('child_process');
var net = require('net');
var server = net.createServer(function(conn) {
var proc = child_process.spawn('cat', [], { stdio: 'pipe' });
conn.pipe(proc.stdin);
proc.stdout.pipe(conn);
});
server.listen(8000);
@bnoordhuis
bnoordhuis / wq.c
Created August 31, 2012 23:24
[libuv] serialized work queue
#include "ngx-queue.h"
#include "uv.h"
struct work
{
ngx_queue_t wq;
uv_work_t req;
int your_data;
};
@bnoordhuis
bnoordhuis / fioclex.c
Created August 21, 2012 21:12
test ioctl() vs fcntl() syscall speed
// cc -O fioclex.c
// USE_IOCTL=0 time ./a.out
// USE_IOCTL=1 time ./a.out
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>