.\" Man page generated from reStructuredText. . .TH "PYPY" "1" "Mar 13, 2019" "7.0" "PyPy" .SH NAME pypy \- fast, compliant alternative implementation of the Python language . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .SH SYNOPSIS .sp \fBpypy\fP [\fIoptions\fP] [\fB\-c\fP \fIcmd\fP|\fB\-m\fP \fImod\fP|\fIfile.py\fP|\fB\-\fP] [\fIarg\fP\&...] .SH OPTIONS .INDENT 0.0 .TP .B \-i Inspect interactively after running script. .TP .B \-O Skip assert statements. .TP .BI \-O\fB O Remove docstrings when importing modules in addition to \fB\-O\fP\&. .TP .BI \-c \ CMD Program passed in as \fBCMD\fP (terminates option list). .TP .B \-S Do not \fBimport site\fP on initialization. .TP .B \-s Don\(aqt add the user site directory to \fIsys.path\fP\&. .TP .B \-u Unbuffered binary \fBstdout\fP and \fBstderr\fP\&. .TP .B \-h\fP,\fB \-\-help Show a help message and exit. .TP .BI \-m \ MOD Library module to be run as a script (terminates option list). .TP .BI \-W \ ARG Warning control (\fIarg\fP is \fIaction\fP:\fImessage\fP:\fIcategory\fP:\fImodule\fP:\fIlineno\fP). .TP .B \-E Ignore environment variables (such as \fBPYTHONPATH\fP). .TP .B \-B Disable writing bytecode (\fB\&.pyc\fP) files. .TP .BI \-X \ track\-resources Produce a \fBResourceWarning\fP whenever a file or socket is closed by the garbage collector. .TP .B \-\-version Print the PyPy version. .TP .B \-\-info Print translation information about this PyPy executable. .TP .BI \-\-jit \ ARG Low level JIT parameters. Mostly internal. Run \fB\-\-jit help\fP for more information. .UNINDENT .SH ENVIRONMENT .INDENT 0.0 .TP .B \fBPYTHONPATH\fP Add directories to pypy\(aqs module search path. The format is the same as shell\(aqs \fBPATH\fP\&. .TP .B \fBPYTHONSTARTUP\fP A script referenced by this variable will be executed before the first prompt is displayed, in interactive mode. .TP .B \fBPYTHONDONTWRITEBYTECODE\fP If set to a non\-empty value, equivalent to the \fB\-B\fP option. Disable writing \fB\&.pyc\fP files. .TP .B \fBPYTHONINSPECT\fP If set to a non\-empty value, equivalent to the \fB\-i\fP option. Inspect interactively after running the specified script. .TP .B \fBPYTHONIOENCODING\fP If this is set, it overrides the encoding used for \fIstdin\fP/\fIstdout\fP/\fIstderr\fP\&. The syntax is \fIencodingname\fP:\fIerrorhandler\fP The \fIerrorhandler\fP part is optional and has the same meaning as in \fIstr.encode\fP\&. .TP .B \fBPYTHONNOUSERSITE\fP If set to a non\-empty value, equivalent to the \fB\-s\fP option. Don\(aqt add the user site directory to \fIsys.path\fP\&. .TP .B \fBPYTHONWARNINGS\fP If set, equivalent to the \fB\-W\fP option (warning control). The value should be a comma\-separated list of \fB\-W\fP parameters. .TP .B \fBPYPYLOG\fP If set to a non\-empty value, enable logging, the format is: .INDENT 7.0 .TP .B \fIfname\fP or \fI+fname\fP logging for profiling: includes all \fBdebug_start\fP/\fBdebug_stop\fP but not any nested \fBdebug_print\fP\&. \fIfname\fP can be \fB\-\fP to log to \fIstderr\fP\&. The \fI+fname\fP form can be used if there is a \fI:\fP in fname .TP .B \fB:\fP\fIfname\fP Full logging, including \fBdebug_print\fP\&. .TP .B \fIprefix\fP\fB:\fP\fIfname\fP Conditional logging. Multiple prefixes can be specified, comma\-separated. Only sections whose name match the prefix will be logged. .UNINDENT .sp \fBPYPYLOG=jit\-log\-opt,jit\-backend:logfile\fP will generate a log suitable for \fIjitviewer\fP, a tool for debugging performance issues under PyPy. .TP .B \fBPYPY_IRC_TOPIC\fP If set to a non\-empty value, print a random #pypy IRC topic at startup of interactive mode. .UNINDENT .sp PyPy\(aqs default garbage collector is called incminimark \- it\(aqs an incremental, generational moving collector. Here we hope to explain a bit how it works and how it can be tuned to suit the workload. .sp Incminimark first allocates objects in so called \fInursery\fP \- place for young objects, where allocation is very cheap, being just a pointer bump. The nursery size is a very crucial variable \- depending on your workload (one or many processes) and cache sizes you might want to experiment with it via \fIPYPY_GC_NURSERY\fP environment variable. When the nursery is full, there is performed a minor collection. Freed objects are no longer referencable and just die, just by not being referenced any more; on the other hand, objects found to still be alive must survive and are copied from the nursery to the old generation. Either to arenas, which are collections of objects of the same size, or directly allocated with malloc if they\(aqre larger. (A third category, the very large objects, are initially allocated outside the nursery and never move.) .sp Since Incminimark is an incremental GC, the major collection is incremental: the goal is not to have any pause longer than 1ms, but in practice it depends on the size and characteristics of the heap: occasionally, there can be pauses between 10\-100ms. .SS Semi\-manual GC management .sp If there are parts of the program where it is important to have a low latency, you might want to control precisely when the GC runs, to avoid unexpected pauses. Note that this has effect only on major collections, while minor collections continue to work as usual. .sp As explained above, a full major collection consists of \fBN\fP steps, where \fBN\fP depends on the size of the heap; generally speaking, it is not possible to predict how many steps will be needed to complete a collection. .sp \fBgc.enable()\fP and \fBgc.disable()\fP control whether the GC runs collection steps automatically. When the GC is disabled the memory usage will grow indefinitely, unless you manually call \fBgc.collect()\fP and \fBgc.collect_step()\fP\&. .sp \fBgc.collect()\fP runs a full major collection. .sp \fBgc.collect_step()\fP runs a single collection step. It returns an object of type \fI\%GcCollectStepStats\fP, the same which is passed to the corresponding \fI\%GC Hooks\fP\&. The following code is roughly equivalent to a \fBgc.collect()\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C while True: if gc.collect_step().major_is_done: break .ft P .fi .UNINDENT .UNINDENT .sp For a real\-world example of usage of this API, you can look at the 3rd\-party module \fI\%pypytools.gc.custom\fP, which also provides a \fBwith customgc.nogc()\fP context manager to mark sections where the GC is forbidden. .SS Fragmentation .sp Before we discuss issues of "fragmentation", we need a bit of precision. There are two kinds of related but distinct issues: .INDENT 0.0 .IP \(bu 2 If the program allocates a lot of memory, and then frees it all by dropping all references to it, then we might expect to see the RSS to drop. (RSS = Resident Set Size on Linux, as seen by "top"; it is an approximation of the actual memory usage from the OS\(aqs point of view.) This might not occur: the RSS may remain at its highest value. This issue is more precisely caused by the process not returning "free" memory to the OS. We call this case "unreturned memory". .IP \(bu 2 After doing the above, if the RSS didn\(aqt go down, then at least future allocations should not cause the RSS to grow more. That is, the process should reuse unreturned memory as long as it has got some left. If this does not occur, the RSS grows even larger and we have real fragmentation issues. .UNINDENT .SS gc.get_stats .sp There is a special function in the \fBgc\fP module called \fBget_stats(memory_pressure=False)\fP\&. .sp \fBmemory_pressure\fP controls whether or not to report memory pressure from objects allocated outside of the GC, which requires walking the entire heap, so it\(aqs disabled by default due to its cost. Enable it when debugging mysterious memory disappearance. .sp Example call looks like that: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C >>> gc.get_stats(True) Total memory consumed: GC used: 4.2MB (peak: 4.2MB) in arenas: 763.7kB rawmalloced: 383.1kB nursery: 3.1MB raw assembler used: 0.0kB memory pressure: 0.0kB \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- Total: 4.2MB Total memory allocated: GC allocated: 4.5MB (peak: 4.5MB) in arenas: 763.7kB rawmalloced: 383.1kB nursery: 3.1MB raw assembler allocated: 0.0kB memory pressure: 0.0kB \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\- Total: 4.5MB .ft P .fi .UNINDENT .UNINDENT .sp In this particular case, which is just at startup, GC consumes relatively little memory and there is even less unused, but allocated memory. In case there is a lot of unreturned memory or actual fragmentation, the "allocated" can be much higher than "used". Generally speaking, "peak" will more closely resemble the actual memory consumed as reported by RSS. Indeed, returning memory to the OS is a hard and not solved problem. In PyPy, it occurs only if an arena is entirely free\-\-\-a contiguous block of 64 pages of 4 or 8 KB each. It is also rare for the "rawmalloced" category, at least for common system implementations of \fBmalloc()\fP\&. .sp The details of various fields: .INDENT 0.0 .IP \(bu 2 GC in arenas \- small old objects held in arenas. If the amount "allocated" is much higher than the amount "used", we have unreturned memory. It is possible but unlikely that we have internal fragmentation here. However, this unreturned memory cannot be reused for any \fBmalloc()\fP, including the memory from the "rawmalloced" section. .IP \(bu 2 GC rawmalloced \- large objects allocated with malloc. This is gives the current (first block of text) and peak (second block of text) memory allocated with \fBmalloc()\fP\&. The amount of unreturned memory or fragmentation caused by \fBmalloc()\fP cannot easily be reported. Usually you can guess there is some if the RSS is much larger than the total memory reported for "GC allocated", but do keep in mind that this total does not include malloc\(aqed memory not known to PyPy\(aqs GC at all. If you guess there is some, consider using \fI\%jemalloc\fP as opposed to system malloc. .UNINDENT .INDENT 0.0 .IP \(bu 2 nursery \- amount of memory allocated for nursery, fixed at startup, controlled via an environment variable .IP \(bu 2 raw assembler allocated \- amount of assembler memory that JIT feels responsible for .IP \(bu 2 memory pressure, if asked for \- amount of memory we think got allocated via external malloc (eg loading cert store in SSL contexts) that is kept alive by GC objects, but not accounted in the GC .UNINDENT .SS GC Hooks .sp GC hooks are user\-defined functions which are called whenever a specific GC event occur, and can be used to monitor GC activity and pauses. You can install the hooks by setting the following attributes: .INDENT 0.0 .TP .B \fBgc.hook.on_gc_minor\fP Called whenever a minor collection occurs. It corresponds to \fBgc\-minor\fP sections inside \fBPYPYLOG\fP\&. .TP .B \fBgc.hook.on_gc_collect_step\fP Called whenever an incremental step of a major collection occurs. It corresponds to \fBgc\-collect\-step\fP sections inside \fBPYPYLOG\fP\&. .TP .B \fBgc.hook.on_gc_collect\fP Called after the last incremental step, when a major collection is fully done. It corresponds to \fBgc\-collect\-done\fP sections inside \fBPYPYLOG\fP\&. .UNINDENT .sp To uninstall a hook, simply set the corresponding attribute to \fBNone\fP\&. To install all hooks at once, you can call \fBgc.hooks.set(obj)\fP, which will look for methods \fBon_gc_*\fP on \fBobj\fP\&. To uninstall all the hooks at once, you can call \fBgc.hooks.reset()\fP\&. .sp The functions called by the hooks receive a single \fBstats\fP argument, which contains various statistics about the event. .sp Note that PyPy cannot call the hooks immediately after a GC event, but it has to wait until it reaches a point in which the interpreter is in a known state and calling user\-defined code is harmless. It might happen that multiple events occur before the hook is invoked: in this case, you can inspect the value \fBstats.count\fP to know how many times the event occurred since the last time the hook was called. Similarly, \fBstats.duration\fP contains the \fBtotal\fP time spent by the GC for this specific event since the last time the hook was called. .sp On the other hand, all the other fields of the \fBstats\fP object are relative only to the \fBlast\fP event of the series. .sp The attributes for \fBGcMinorStats\fP are: .INDENT 0.0 .TP .B \fBcount\fP The number of minor collections occurred since the last hook call. .TP .B \fBduration\fP The total time spent inside minor collections since the last hook call. See below for more information on the unit. .TP .B \fBduration_min\fP The duration of the fastest minor collection since the last hook call. .TP .B \fBduration_max\fP .INDENT 7.0 .INDENT 3.5 The duration of the slowest minor collection since the last hook call. .UNINDENT .UNINDENT .INDENT 7.0 .TP .B \fBtotal_memory_used\fP The amount of memory used at the end of the minor collection, in bytes. This include the memory used in arenas (for GC\-managed memory) and raw\-malloced memory (e.g., the content of numpy arrays). .UNINDENT .TP .B \fBpinned_objects\fP the number of pinned objects. .UNINDENT .sp The attributes for \fBGcCollectStepStats\fP are: .INDENT 0.0 .TP .B \fBcount\fP, \fBduration\fP, \fBduration_min\fP, \fBduration_max\fP See above. .TP .B \fBoldstate\fP, \fBnewstate\fP Integers which indicate the state of the GC before and after the step. .TP .B \fBmajor_is_done\fP Boolean which indicate whether this was the last step of the major collection .UNINDENT .sp The value of \fBoldstate\fP and \fBnewstate\fP is one of these constants, defined inside \fBgc.GcCollectStepStats\fP: \fBSTATE_SCANNING\fP, \fBSTATE_MARKING\fP, \fBSTATE_SWEEPING\fP, \fBSTATE_FINALIZING\fP, \fBSTATE_USERDEL\fP\&. It is possible to get a string representation of it by indexing the \fBGC_STATES\fP tuple. .sp The attributes for \fBGcCollectStats\fP are: .INDENT 0.0 .TP .B \fBcount\fP See above. .TP .B \fBnum_major_collects\fP The total number of major collections which have been done since the start. Contrarily to \fBcount\fP, this is an always\-growing counter and it\(aqs not reset between invocations. .TP .B \fBarenas_count_before\fP, \fBarenas_count_after\fP Number of arenas used before and after the major collection. .TP .B \fBarenas_bytes\fP Total number of bytes used by GC\-managed objects. .TP .B \fBrawmalloc_bytes_before\fP, \fBrawmalloc_bytes_after\fP Total number of bytes used by raw\-malloced objects, before and after the major collection. .UNINDENT .sp Note that \fBGcCollectStats\fP has \fBnot\fP got a \fBduration\fP field. This is because all the GC work is done inside \fBgc\-collect\-step\fP: \fBgc\-collect\-done\fP is used only to give additional stats, but doesn\(aqt do any actual work. .sp A note about the \fBduration\fP field: depending on the architecture and operating system, PyPy uses different ways to read timestamps, so \fBduration\fP is expressed in varying units. It is possible to know which by calling \fB__pypy__.debug_get_timestamp_unit()\fP, which can be one of the following values: .INDENT 0.0 .TP .B \fBtsc\fP The default on \fBx86\fP machines: timestamps are expressed in CPU ticks, as read by the \fI\%Time Stamp Counter\fP\&. .TP .B \fBns\fP Timestamps are expressed in nanoseconds. .TP .B \fBQueryPerformanceCounter\fP On Windows, in case for some reason \fBtsc\fP is not available: timestamps are read using the win API \fBQueryPerformanceCounter()\fP\&. .UNINDENT .sp Unfortunately, there does not seem to be a reliable standard way for converting \fBtsc\fP ticks into nanoseconds, although in practice on modern CPUs it is enough to divide the ticks by the maximum nominal frequency of the CPU. For this reason, PyPy gives the raw value, and leaves the job of doing the conversion to external libraries. .sp Here is an example of GC hooks in use: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C import sys import gc class MyHooks(object): done = False def on_gc_minor(self, stats): print \(aqgc\-minor: count = %02d, duration = %d\(aq % (stats.count, stats.duration) def on_gc_collect_step(self, stats): old = gc.GcCollectStepStats.GC_STATES[stats.oldstate] new = gc.GcCollectStepStats.GC_STATES[stats.newstate] print \(aqgc\-collect\-step: %s \-\-> %s\(aq % (old, new) print \(aq count = %02d, duration = %d\(aq % (stats.count, stats.duration) def on_gc_collect(self, stats): print \(aqgc\-collect\-done: count = %02d\(aq % stats.count self.done = True hooks = MyHooks() gc.hooks.set(hooks) # simulate some GC activity lst = [] while not hooks.done: lst = [lst, 1, 2, 3] .ft P .fi .UNINDENT .UNINDENT .SS Environment variables .sp PyPy\(aqs default \fBincminimark\fP garbage collector is configurable through several environment variables: .INDENT 0.0 .TP .B \fBPYPY_GC_NURSERY\fP The nursery size. Defaults to 1/2 of your last\-level cache, or \fB4M\fP if unknown. Small values (like 1 or 1KB) are useful for debugging. .TP .B \fBPYPY_GC_NURSERY_DEBUG\fP If set to non\-zero, will fill nursery with garbage, to help debugging. .TP .B \fBPYPY_GC_INCREMENT_STEP\fP The size of memory marked during the marking step. Default is size of nursery times 2. If you mark it too high your GC is not incremental at all. The minimum is set to size that survives minor collection times 1.5 so we reclaim anything all the time. .TP .B \fBPYPY_GC_MAJOR_COLLECT\fP Major collection memory factor. Default is \fB1.82\fP, which means trigger a major collection when the memory consumed equals 1.82 times the memory really used at the end of the previous major collection. .TP .B \fBPYPY_GC_GROWTH\fP Major collection threshold\(aqs max growth rate. Default is \fB1.4\fP\&. Useful to collect more often than normally on sudden memory growth, e.g. when there is a temporary peak in memory usage. .TP .B \fBPYPY_GC_MAX\fP The max heap size. If coming near this limit, it will first collect more often, then raise an RPython MemoryError, and if that is not enough, crash the program with a fatal error. Try values like \fB1.6GB\fP\&. .TP .B \fBPYPY_GC_MAX_DELTA\fP The major collection threshold will never be set to more than \fBPYPY_GC_MAX_DELTA\fP the amount really used after a collection. Defaults to 1/8th of the total RAM size (which is constrained to be at most 2/3/4GB on 32\-bit systems). Try values like \fB200MB\fP\&. .TP .B \fBPYPY_GC_MIN\fP Don\(aqt collect while the memory size is below this limit. Useful to avoid spending all the time in the GC in very small programs. Defaults to 8 times the nursery. .TP .B \fBPYPY_GC_DEBUG\fP Enable extra checks around collections that are too slow for normal use. Values are \fB0\fP (off), \fB1\fP (on major collections) or \fB2\fP (also on minor collections). .TP .B \fBPYPY_GC_MAX_PINNED\fP The maximal number of pinned objects at any point in time. Defaults to a conservative value depending on nursery size and maximum object size inside the nursery. Useful for debugging by setting it to 0. .UNINDENT .SH SEE ALSO .sp \fBpython\fP(1) .SH AUTHOR The PyPy Project .SH COPYRIGHT 2019, The PyPy Project .\" Generated by docutils manpage writer. .