Skip to content

Instantly share code, notes, and snippets.

View jpauli's full-sized avatar
🐍

jpauli jpauli

🐍
View GitHub Profile
@jpauli
jpauli / gist:2158791
Created March 22, 2012 14:54
Simple PHP chat using nc
<?php
$p = proc_open("/bin/nc -l -p 11111", array (STDIN, array("pipe", "w"), array("file", "/dev/null", 'w')), $pipes);
while (1) {
$read = array($pipes[1]);
$write = $except = null;
stream_select($read, $write, $except, 2);
echo fgets($pipes[1]);
}
@jpauli
jpauli / gist:2471030
Created April 23, 2012 13:50
libmysql VS mysqlnd read-only resultset allocations with buffered queries (store mode)
---- libmysql in store result mode ----
206 1,035,035,700 24,367,728 24,177,686 190,042 0
99.22% (24,177,686B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->88.15% (21,479,147B) 0x52DD010: my_malloc (my_malloc.c:37)
| ->88.04% (21,452,640B) 0x52E0583: alloc_root (my_alloc.c:219)
| | ->44.60% (10,869,120B) 0x5307A1F: cli_read_rows (client.c:1416)
| | | ->44.60% (10,869,120B) 0x5305955: mysql_store_result (client.c:2957)
| | | | ->44.60% (10,869,120B) 0x53EF09: zif_mysqli_query (mysqli_nonapi.c:540)
| | | | ->44.60% (10,869,120B) 0x70186A: zend_do_fcall_common_helper_SPEC (zend_vm_execute.h:320)
@jpauli
jpauli / gist:2480585
Created April 24, 2012 15:25
Using mysqlnd stats to track development errors
<?php
class ComutoMysqli extends Mysqli
{
public function __destruct()
{
$stats = $this->get_connection_stats();
$this->close();
if($diff = $stats["rows_fetched_from_server_normal"] - ($stats["rows_fetched_from_client_normal_unbuffered"] + $stats["rows_fetched_from_client_normal_buffered"])) {
trigger_error("You didn't use *$diff* selected results, that's wasted!", E_USER_NOTICE);
}
@jpauli
jpauli / gist:2499452
Created April 26, 2012 13:11
mysqlnd read-only copy-on-write memory savings
julien@lapjpauli:/tmp$ valgrind /home/julien/php53mysqlnd/bin/php -dmemory_limit=-1 /home/julien/www/mysqlnd_POC/poc_mysqlnd.php 1000
initial memory ->86480 kB
resultSet stored ->229632 kB
query result saved ->234016 kB
query result freed ->234016 kB
resultSet freed ->134816 kB
Db closed ->134816 kB
==32226==
==32226== HEAP SUMMARY:
==32226== in use at exit: 0 bytes in 0 blocks
@jpauli
jpauli / gist:3236041
Created August 2, 2012 10:01
Save your I/Os, dont use open_basedir
$> strace php -r '@unlink("foo.bar");' 2>&1|grep 'foo.bar'
execve("/usr/bin/php", ["php", "-r", "@unlink(\"foo.bar\");"], [/* 17 vars */]) = 0
unlink("foo.bar") = -1 ENOENT (No such file or directory)
$> strace php -d open_basedir="./" -r '@unlink("foo.bar");' 2>&1|grep 'foo.bar'
typedef struct _hashtable {
uint nTableSize;
uint nTableMask;
uint nNumOfElements;
zend_bool persistent;
unsigned char nApplyCount;
zend_bool bApplyProtection;
ulong nNextFreeElement;
Bucket *pInternalPointer; /* Used for element traversal */
diff --git a/xdebug_var.c b/xdebug_var.c
index a59c4f5..c92d114 100644
diff --git a/xdebug_var.c b/xdebug_var.c
index a59c4f5..c92d114 100644
--- a/xdebug_var.c
+++ b/xdebug_var.c
@@ -434,6 +434,7 @@ void xdebug_var_export(zval **struc, xdebug_str *str, int level, int debug_zval,
case IS_OBJECT:
myht = Z_OBJPROP_PP(struc);
@jpauli
jpauli / gist:5466273
Created April 26, 2013 10:13
ZEND_MM_TRUE_SIZE expanded on my platform , needs 123 steps
((size<(((((((((sizeof(zend_mm_block)) + 8 - 1) & ~(8-1)) + sizeof(unsigned int)) + 8 - 1) & ~(8-1))>(((sizeof(zend_mm_small_free_block)) + 8 - 1) & ~(8-1))?((((((sizeof(zend_mm_block)) + 8 - 1) & ~(8-1)) + sizeof(unsigned int)) + 8 - 1) & ~(8-1)):(((sizeof(zend_mm_small_free_block)) + 8 - 1) & ~(8-1)))>((((sizeof(zend_mm_block)) + 8 - 1) & ~(8-1))+sizeof(unsigned int)))?((((((((sizeof(zend_mm_block)) + 8 - 1) & ~(8-1)) + sizeof(unsigned int)) + 8 - 1) & ~(8-1))>(((sizeof(zend_mm_small_free_block)) + 8 - 1) & ~(8-1))?((((((sizeof(zend_mm_block)) + 8 - 1) & ~(8-1)) + sizeof(unsigned int)) + 8 - 1) & ~(8-1)):(((sizeof(zend_mm_small_free_block)) + 8 - 1) & ~(8-1)))-((((sizeof(zend_mm_block)) + 8 - 1) & ~(8-1))+sizeof(unsigned int))):0))?((((((((sizeof(zend_mm_block)) + 8 - 1) & ~(8-1)) + sizeof(unsigned int)) + 8 - 1) & ~(8-1))>(((sizeof(zend_mm_small_free_block)) + 8 - 1) & ~(8-1))?((((((sizeof(zend_mm_block)) + 8 - 1) & ~(8-1)) + sizeof(unsigned int)) + 8 - 1) & ~(8-1)):(((sizeof(zend_mm_small_free_block)) + 8
@jpauli
jpauli / gist:6230757
Last active December 21, 2015 01:49
libpcap embedding into PHP
<?php
$r = phpcap_create('eth0', PHPCAP_DEV_PROMISC);
phpcap_dispatch($r, function($packet) {
$unpack = unpack('H12macdest/H12macsrc/H4ethtype/H24/a4ipsrc/a4ipdest',$packet);
printf("Got a packet, from %s(%s) to %s(%s)\n",
inet_ntop($unpack['ipsrc']),
$unpack['macsrc'],
$o1 = new stdclass($a=8);
var_dump($a); // Notice: Undefined variable: a
$o2 = new Datetime($b='now');
var_dump($b); // string "now"