• Stars
    star
    428
  • Rank 101,481 (Top 2 %)
  • Language
    C
  • License
    Other
  • Created over 5 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Detect unused code in production

Tombs

Build Status

Tombs is a Zend extension for PHP that provides a way to track if a piece of code is actually invoked. This concept is called Tombstones.
It uses Zend hooks to populate a graveyard at runtime, wherein a tomb is representative of every function that Zend has constructed. As Zend executes, functions that are used have their tombs vacated. When the programmer connects to the Tombs socket, a background thread will send populated tombs without interrupting the execution of your application.

Requirements

  • PHP7.1+
  • unix

Note: Windows is unsupported, Tombs targets production ...

How To

Here is a quick run down of how to use Tombs ...

To build Tombs:

  • phpize
  • ./configure [--with-php-config=/path/to/php-config]
  • make
  • make install

To load Tombs:

Tombs must be loaded as a Zend extension:

  • add zend_extension=tombs.so to the target configuration

php -v should show something like: ... with Tombs vX.X.X-X, Copyright (c) 2019, by krakjoe

To configure Tombs:

The following configuration directives are available:

Name Default Purpose
tombs.slots 10000 Set to (a number greater than) the maximum number of functions
tombs.strings 32M Set size of string buffer (supports suffixes, be generous)
tombs.socket zend.tombs.socket Set path to socket, setting to 0 disables socket
tombs.dump 0 Set to a file descriptor for dump on shutdown
tombs.namespace N/A Set to restrict recording to a namespace
tombs.graveyard_format json Format of graveyard: json, function

To communicate with Tombs:

Tombs can be configured to communicate via a unix or TCP socket, the following are valid examples:

  • unix://zend.tombs.socket
  • unix:///var/run/zend.tombs.socket
  • tcp://127.0.0.1:8010
  • tcp://localhost:8010

Note: If the scheme is omitted, the scheme is assumed to be unix

Tombs will send each populated tomb as a json encoded packet, with one tomb per line, with the following format, prettified for readability (tombs.graveyard_format=json):

{
    "location": {
        "file":  "004.php",
        "start": 5,
        "end":   6
    },
    "scope": "Foo",
    "function": "bar"
}

Note: The scope element will only be present for methods

Output format "function"

For more compact output use tombs.graveyard_format=function:

Foo::bar

Internals

On startup (MINIT) Tombs maps three regions of memory:

  • Markers - a pointer to zend_bool in the reserved region of every op array
  • Strings - region of memory for copying persistent strings: file names, class names, and function names
  • Graveyard - a tomb for each possible function

All memory is shared among forks and threads, and Tombs uses atomics, for maximum glory.

Should mapping fail, because there isn't enough memory for example, Tombs will not stop the process from starting up but will only output a warning. Should mapping succeed, the configured socket will be opened. Should opening the socket fail, Tombs will be shutdown immediately but allow the process to continue.

Markers

The op array constructor hook for zend extensions is used to set reserved memory to a mapped pointer to zend_bool. If the atomic set succeeds, the hook then populates a tomb in the graveyard.

Strings

When Tombs needs to reference a string, it is copied from its current location and into mapped memory. These copied strings are reused, and are indepdenent of the request cycle of PHP, and opcache.

Once allocated, a string is never free'd. We must be sure that even after the request has ended or opcache restarted, the string is available. We must also be sure that should there be a client connected to the socket we do not free memory that is being written to the socket.

Graveyard

The executor function that Tombs installs updates the value of the marker when the function is entered. If the atomic set succeeds, the tomb is vacated.

TODO

  • Tests
  • CI

More Repositories

1

pthreads

Threading for PHP - Share Nothing, Do Everything :)
C
3,471
star
2

parallel

A succinct parallel concurrency API for PHP8
C
1,448
star
3

apcu

APCu - APC User Cache
C
964
star
4

phpdbg

The Interactive PHP Debugger
C
837
star
5

pcov

PCOV - CodeCoverage compatible driver for PHP
C
695
star
6

ui

Cross platform UI development in PHP
C
516
star
7

uopz

User Operations for Zend
C
354
star
8

stat

A super modern high performance profiler for production
C
209
star
9

jitfu

Creating native instructions in PHP since 2014
C
191
star
10

explain

Explain
JavaScript
110
star
11

pthreads-polyfill

A polyfill for pthreads
PHP
80
star
12

ilimit

Limit time and memory consumption of individual calls
C
70
star
13

idbg

Inspector Debugger
PHP
70
star
14

wkhtmltox

Converting HTML to X since 2017
C
66
star
15

promises

Promises in PHP
PHP
64
star
16

ustring

UnicodeString for PHP7
C++
64
star
17

autostrict

Automatic strict types in PHP7
C
64
star
18

inspector

Disassembler and Debug Kit for PHP 7
C
55
star
19

sandbox

A sandbox environment for PHP7.1+
C
54
star
20

cmark

CommonMark for PHP
C
46
star
21

mimus

mocking framework as light as a bird ...
PHP
46
star
22

pcov-clobber

Run PCOV in versions of PHPUnit before 8, if you must ...
PHP
40
star
23

componere

Complex Type Composition and Manipulation
C
39
star
24

apcu-bc

APCu Backwards Compatiblity Module
C
33
star
25

SIMD

Single Instruction, Multiple Data
C
30
star
26

pthreads-autoloading-composer

An example of how to use composers autoloader in conjunction with Pools
PHP
29
star
27

memoize

Caching the result of your expensive function calls since 2016
C
23
star
28

profiler

An extension to profile PHP
C
19
star
29

php-jansson

jannson based json encoder and decoder for PHP
C
18
star
30

trace

Tracing for PHP7 Processes
C
18
star
31

snappy

Snappy Compression for PHP
PHP
15
star
32

indexed

Educational Extension for PHP7
C
13
star
33

router

This extension serves to provide a sane, easy router for modern PHP applications:
C
13
star
34

uref

weak refs, for the brave ...
C++
13
star
35

phpdbg-ui

phpdbg remote console client
Java
13
star
36

ponion

onion server for php
C
10
star
37

operators

Override operators in userland
C
9
star
38

HandlerSocket

HandlerSocket PHP 7 Extension
C
9
star
39

apcup

APCu Pooling
C
9
star
40

utypes

User verified types
C
9
star
41

overload

Overloading Zend (RESEARCH)
C
9
star
42

bundle

An experimental method of bundling PHP code with extensions
C
7
star
43

nocheq

Sick of pesky type checking making your code too slow (and correct) ?
C
7
star
44

kore

kore php functions
C
6
star
45

perf

Sampling Profiler for PHP 7 (Unfinished)
C
6
star
46

Object

Multiple Inheritance at Runtime
C
5
star
47

cmark-visitors

Visitors for CommonMark implementing some useful AST transformations
PHP
4
star
48

instrumental

Componere Instrumental
PHP
4
star
49

u2fh

PHP7 u2fh
C
4
star
50

u2fs

PHP7 u2fs
C
3
star
51

sysconf

Get configuration information at run time
C
3
star
52

jitfi

PHP FFI header for libjit
C
3
star
53

cloname

Demonstrate how to give closures long names
C
2
star
54

dec64

dec64
Assembly
2
star
55

transformer

C
2
star
56

zi

Zend Instruments
C
2
star
57

ohash

spl_object_hash alternative playground
C
1
star
58

ui-doc

Documentation Resources for UI
PHP
1
star
59

apcu-ps

APCu PS Module
C
1
star