| 1 | '\" | 
|---|
| 2 | '\" Copyright (c) 2004 Donal K. Fellows | 
|---|
| 3 | '\" | 
|---|
| 4 | '\" See the file "license.terms" for information on usage and redistribution | 
|---|
| 5 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. | 
|---|
| 6 | '\"  | 
|---|
| 7 | '\" RCS: @(#) $Id: Limit.3,v 1.7 2004/11/12 09:01:25 das Exp $ | 
|---|
| 8 | '\"  | 
|---|
| 9 | .so man.macros | 
|---|
| 10 | .TH Tcl_LimitCheck 3 8.5 Tcl "Tcl Library Procedures" | 
|---|
| 11 | .BS | 
|---|
| 12 | .SH NAME | 
|---|
| 13 | Tcl_LimitAddHandler, Tcl_LimitCheck, Tcl_LimitExceeded, Tcl_LimitGetCommands, Tcl_LimitGetGranularity, Tcl_LimitGetTime, Tcl_LimitReady, Tcl_LimitRemoveHandler, Tcl_LimitSetCommands, Tcl_LimitSetGranularity, Tcl_LimitSetTime, Tcl_LimitTypeEnabled, Tcl_LimitTypeExceeded, Tcl_LimitTypeReset, Tcl_LimitTypeSet \- manage and check resource limits on interpreters | 
|---|
| 14 | .SH SYNOPSIS | 
|---|
| 15 | .nf | 
|---|
| 16 | \fB#include <tcl.h>\fR | 
|---|
| 17 | .sp | 
|---|
| 18 | int | 
|---|
| 19 | \fBTcl_LimitCheck\fR(\fIinterp\fR) | 
|---|
| 20 | .sp | 
|---|
| 21 | int | 
|---|
| 22 | \fBTcl_LimitReady\fR(\fIinterp\fR) | 
|---|
| 23 | .sp | 
|---|
| 24 | int | 
|---|
| 25 | \fBTcl_LimitExceeded\fR(\fIinterp\fR) | 
|---|
| 26 | .sp | 
|---|
| 27 | int | 
|---|
| 28 | \fBTcl_LimitTypeExceeded\fR(\fIinterp, type\fR) | 
|---|
| 29 | .sp | 
|---|
| 30 | int | 
|---|
| 31 | \fBTcl_LimitTypeEnabled\fR(\fIinterp, type\fR) | 
|---|
| 32 | .sp | 
|---|
| 33 | void | 
|---|
| 34 | \fBTcl_LimitTypeSet\fR(\fIinterp, type\fR) | 
|---|
| 35 | .sp | 
|---|
| 36 | void | 
|---|
| 37 | \fBTcl_LimitTypeReset\fR(\fIinterp, type\fR) | 
|---|
| 38 | .sp | 
|---|
| 39 | int | 
|---|
| 40 | \fBTcl_LimitGetCommands\fR(\fIinterp\fR) | 
|---|
| 41 | .sp | 
|---|
| 42 | void | 
|---|
| 43 | \fBTcl_LimitSetCommands\fR(\fIinterp, commandLimit\fR) | 
|---|
| 44 | .sp | 
|---|
| 45 | void | 
|---|
| 46 | \fBTcl_LimitGetTime\fR(\fIinterp, timeLimitPtr\fR) | 
|---|
| 47 | .sp | 
|---|
| 48 | void | 
|---|
| 49 | \fBTcl_LimitSetTime\fR(\fIinterp, timeLimitPtr\fR) | 
|---|
| 50 | .sp | 
|---|
| 51 | int | 
|---|
| 52 | \fBTcl_LimitGetGranularity\fR(\fIinterp, type\fR) | 
|---|
| 53 | .sp | 
|---|
| 54 | void | 
|---|
| 55 | \fBTcl_LimitSetGranularity\fR(\fIinterp, type, granularity\fR) | 
|---|
| 56 | .sp | 
|---|
| 57 | void | 
|---|
| 58 | \fBTcl_LimitAddHandler\fR(\fIinterp, type, handlerProc, clientData, deleteProc\fR) | 
|---|
| 59 | .sp | 
|---|
| 60 | void | 
|---|
| 61 | \fBTcl_LimitRemoveHandler\fR(\fIinterp, type, handlerProc, clientData\fR) | 
|---|
| 62 | .SH ARGUMENTS | 
|---|
| 63 | .AS Tcl_LimitHandlerDeleteProc commandLimit in/out | 
|---|
| 64 | .AP Tcl_Interp *interp in | 
|---|
| 65 | Interpreter that the limit being managed applies to or that will have | 
|---|
| 66 | its limits checked. | 
|---|
| 67 | .AP int type in | 
|---|
| 68 | The type of limit that the operation refers to.  This must be either | 
|---|
| 69 | \fBTCL_LIMIT_COMMANDS\fR or \fBTCL_LIMIT_TIME\fR. | 
|---|
| 70 | .AP int commandLimit in | 
|---|
| 71 | The maximum number of commands (as reported by \fBinfo cmdcount\fR) | 
|---|
| 72 | that may be executed in the interpreter. | 
|---|
| 73 | .AP Tcl_Time *timeLimitPtr in/out | 
|---|
| 74 | A pointer to a structure that will either have the new time limit read | 
|---|
| 75 | from (\fBTcl_LimitSetTime\fR) or the current time limit written to | 
|---|
| 76 | (\fBTcl_LimitGetTime\fR). | 
|---|
| 77 | .AP int granularity in | 
|---|
| 78 | Divisor that indicates how often a particular limit should really be | 
|---|
| 79 | checked.  Must be at least 1. | 
|---|
| 80 | .AP Tcl_LimitHandlerProc *handlerProc in | 
|---|
| 81 | Function to call when a particular limit is exceeded.  If the | 
|---|
| 82 | \fIhandlerProc\fR removes or raises the limit during its processing, | 
|---|
| 83 | the limited interpreter will be permitted to continue to process after | 
|---|
| 84 | the handler returns.  Many handlers may be attached to the same | 
|---|
| 85 | interpreter limit; their order of execution is not defined, and they | 
|---|
| 86 | must be identified by \fIhandlerProc\fR and \fIclientData\fR when they | 
|---|
| 87 | are deleted. | 
|---|
| 88 | .AP ClientData clientData in | 
|---|
| 89 | Arbitrary pointer-sized word used to pass some context to the | 
|---|
| 90 | \fIhandlerProc\fR function. | 
|---|
| 91 | .AP Tcl_LimitHandlerDeleteProc *deleteProc in | 
|---|
| 92 | Function to call whenever a handler is deleted.  May be NULL if the | 
|---|
| 93 | \fIclientData\fR requires no deletion. | 
|---|
| 94 | .BE | 
|---|
| 95 |  | 
|---|
| 96 | .SH DESCRIPTION | 
|---|
| 97 | .PP | 
|---|
| 98 | Tcl's interpreter resource limit subsystem allows for close control | 
|---|
| 99 | over how much computation time a script may use, and is useful for | 
|---|
| 100 | cases where a program is divided into multiple pieces where some parts | 
|---|
| 101 | are more trusted than others (e.g. web application servers). | 
|---|
| 102 | .PP | 
|---|
| 103 | Every interpreter may have a limit on the wall-time for execution, and | 
|---|
| 104 | a limit on the number of commands that the interpreter may execute. | 
|---|
| 105 | Since checking of these limits is potentially expensive (especially | 
|---|
| 106 | the time limit), each limit also has a checking granularity, which is | 
|---|
| 107 | a divisor for an internal count of the number of points in the core | 
|---|
| 108 | where a check may be performed (which is immediately before executing | 
|---|
| 109 | a command and at an unspecified frequency between running commands, | 
|---|
| 110 | which can happen in empty-bodied \fBwhile\fR loops). | 
|---|
| 111 | .PP | 
|---|
| 112 | The final component of the limit engine is a callback scheme which | 
|---|
| 113 | allows for notifications of when a limit has been exceeded.  These | 
|---|
| 114 | callbacks can just provide logging, or may allocate more resources to | 
|---|
| 115 | the interpreter to permit it to continue processing longer. | 
|---|
| 116 | .PP | 
|---|
| 117 | When a limit is exceeded (and the callbacks have run; the order of | 
|---|
| 118 | execution of the callbacks is unspecified) execution in the limited | 
|---|
| 119 | interpreter is stopped by raising an error and setting a flag that | 
|---|
| 120 | prevents the \fBcatch\fR command in that interpreter from trapping | 
|---|
| 121 | that error.  It is up to the context that started execution in that | 
|---|
| 122 | interpreter (typically a master interpreter) to handle the error. | 
|---|
| 123 | .SH "LIMIT CHECKING API" | 
|---|
| 124 | .PP | 
|---|
| 125 | To check the resource limits for an interpreter, call | 
|---|
| 126 | \fBTcl_LimitCheck\fR, which returns \fBTCL_OK\fR if the limit was not | 
|---|
| 127 | exceeded (after processing callbacks) and \fBTCL_ERROR\fR if the limit was | 
|---|
| 128 | exceeded (in which case an error message is also placed in the | 
|---|
| 129 | interpreter result).  That function should only be called when | 
|---|
| 130 | \fBTcl_LimitReady\fR returns non-zero so that granularity policy is | 
|---|
| 131 | enforced.  This API is designed to be similar in usage to | 
|---|
| 132 | \fBTcl_AsyncReady\fR and \fBTcl_AsyncInvoke\fR. | 
|---|
| 133 | .PP | 
|---|
| 134 | When writing code that may behave like \fBcatch\fR in respect of | 
|---|
| 135 | errors, you should only trap an error if \fBTcl_LimitExceeded\fR | 
|---|
| 136 | returns zero.  If it returns non-zero, the interpreter is in a | 
|---|
| 137 | limit-exceeded state and errors should be allowed to propagate to the | 
|---|
| 138 | calling context.  You can also check whether a particular type of | 
|---|
| 139 | limit has been exceeded using \fBTcl_LimitTypeExceeded\fR. | 
|---|
| 140 | .SH "LIMIT CONFIGURATION" | 
|---|
| 141 | .PP | 
|---|
| 142 | To check whether a limit has been set (but not whether it has actually | 
|---|
| 143 | been exceeded) on an interpreter, call \fBTcl_LimitTypeEnabled\fR with | 
|---|
| 144 | the type of limit you want to check.  To enable a particular limit | 
|---|
| 145 | call \fBTcl_LimitTypeSet\fR, and to disable a limit call | 
|---|
| 146 | \fBTcl_LimitTypeReset\fR. | 
|---|
| 147 | .PP | 
|---|
| 148 | The level of a command limit may be set using | 
|---|
| 149 | \fBTcl_LimitSetCommands\fR, and retrieved using | 
|---|
| 150 | \fBTcl_LimitGetCommands\fR.  Similarly for a time limit with | 
|---|
| 151 | \fBTcl_LimitSetTime\fR and \fBTcl_LimitGetTime\fR respectively, but | 
|---|
| 152 | with that API the time limit is copied from and to the Tcl_Time | 
|---|
| 153 | structure that the \fItimeLimitPtr\fR argument points to. | 
|---|
| 154 | .PP | 
|---|
| 155 | The checking granularity for a particular limit may be set using | 
|---|
| 156 | \fBTcl_LimitSetGranularity\fR and retrieved using | 
|---|
| 157 | \fBTcl_LimitGetGranularity\fR.  Note that granularities must always be | 
|---|
| 158 | positive. | 
|---|
| 159 | .SS "LIMIT CALLBACKS" | 
|---|
| 160 | .PP | 
|---|
| 161 | To add a handler callback to be invoked when a limit is exceeded, call | 
|---|
| 162 | \fBTcl_LimitAddHandler\fR.  The \fIhandlerProc\fR argument describes | 
|---|
| 163 | the function that will actually be called; it should have the | 
|---|
| 164 | following prototype: | 
|---|
| 165 | .PP | 
|---|
| 166 | .CS | 
|---|
| 167 | typedef void Tcl_LimitHandlerProc( | 
|---|
| 168 |         ClientData \fIclientData\fR, | 
|---|
| 169 |         Tcl_Interp *\fIinterp\fR); | 
|---|
| 170 | .CE | 
|---|
| 171 | .PP | 
|---|
| 172 | The \fIclientData\fR argument to the handler will be whatever is | 
|---|
| 173 | passed to the \fIclientData\fR argment to \fBTcl_LimitAddHandler\fR, | 
|---|
| 174 | and the \fIinterp\fR is the interpreter that had its limit exceeded. | 
|---|
| 175 | .PP | 
|---|
| 176 | The \fIdeleteProc\fR argument to \fBTcl_LimitAddHandler\fR is a | 
|---|
| 177 | function to call to delete the \fIclientData\fR value.  It may be | 
|---|
| 178 | \fBTCL_STATIC\fR or NULL if no deletion action is necessary, or | 
|---|
| 179 | \fBTCL_DYNAMIC\fR if all that is necessary is to free the structure with | 
|---|
| 180 | \fBTcl_Free\fR.  Otherwise, it should refer to a function with the | 
|---|
| 181 | following prototype: | 
|---|
| 182 | .PP | 
|---|
| 183 | .CS | 
|---|
| 184 | typedef void Tcl_LimitHandlerDeleteProc( | 
|---|
| 185 |         ClientData \fIclientData\fR); | 
|---|
| 186 | .CE | 
|---|
| 187 | .PP | 
|---|
| 188 | A limit handler may be deleted using \fBTcl_LimitRemoveHandler\fR; the | 
|---|
| 189 | handler removed will be the first one found (out of the handlers added | 
|---|
| 190 | with \fBTcl_LimitAddHandler\fR) with exactly matching \fItype\fR, | 
|---|
| 191 | \fIhandlerProc\fR and \fIclientData\fR arguments.  This function | 
|---|
| 192 | always invokes the \fIdeleteProc\fR on the \fIclientData\fR (unless | 
|---|
| 193 | the \fIdeleteProc\fR was NULL or \fBTCL_STATIC\fR). | 
|---|
| 194 |  | 
|---|
| 195 | .SH KEYWORDS | 
|---|
| 196 | interpreter, resource, limit, commands, time, callback | 
|---|