Debian Codegraph @

doc/Limit.3

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