Whamcloud - gitweb
LU-969 debug: reduce stack usage
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_private.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * libcfs/include/libcfs/libcfs_private.h
39  *
40  * Various defines for libcfs.
41  *
42  */
43
44 #ifndef __LIBCFS_PRIVATE_H__
45 #define __LIBCFS_PRIVATE_H__
46
47 /* XXX this layering violation is for nidstrings */
48 #include <lnet/types.h>
49
50 #ifndef DEBUG_SUBSYSTEM
51 # define DEBUG_SUBSYSTEM S_UNDEFINED
52 #endif
53
54 #ifdef __KERNEL__
55
56 #ifdef LIBCFS_DEBUG
57
58 /*
59  * When this is on, LASSERT macro includes check for assignment used instead
60  * of equality check, but doesn't have unlikely(). Turn this on from time to
61  * time to make test-builds. This shouldn't be on for production release.
62  */
63 #define LASSERT_CHECKED (0)
64
65 #if LASSERT_CHECKED
66 /*
67  * Assertion.
68  *
69  * Strange construction with empty "then" clause is used to trigger compiler
70  * warnings on the assertions of the form LASSERT(a = b);
71  *
72  * "warning: suggest parentheses around assignment used as truth value"
73  *
74  * requires -Wall. Unfortunately this rules out use of likely/unlikely.
75  */
76 #define LASSERTF(cond, fmt, ...)                                        \
77 do {                                                                    \
78         if (cond)                                                       \
79                 ;                                                       \
80         else {                                                          \
81                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL);     \
82                 libcfs_debug_msg(&msgdata,                              \
83                                  "ASSERTION( %s ) failed: " fmt, #cond, \
84                                   ## __VA_ARGS__);                      \
85                 LBUG();                                                 \
86         }                                                              \
87 } while(0)
88
89 #define LASSERT(cond) LASSERTF(cond, "\n")
90
91 #else /* !LASSERT_CHECKED */
92
93 #define LASSERTF(cond, fmt, ...)                                        \
94 do {                                                                    \
95         if (unlikely(!(cond))) {                                        \
96                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL);     \
97                 libcfs_debug_msg(&msgdata,                              \
98                                  "ASSERTION( %s ) failed: " fmt, #cond, \
99                                  ## __VA_ARGS__ );                      \
100                 LBUG();                                                 \
101         }                                                               \
102 } while(0)
103
104 #define LASSERT(cond) LASSERTF(cond, "\n")
105 #endif /* !LASSERT_CHECKED */
106 #else /* !LIBCFS_DEBUG */
107 /* sizeof is to use expression without evaluating it. */
108 # define LASSERT(e) ((void)sizeof!!(e))
109 # define LASSERTF(cond, ...) ((void)sizeof!!(cond))
110 #endif /* !LIBCFS_DEBUG */
111
112 #ifdef INVARIANT_CHECK
113 /**
114  * This is for more expensive checks that one doesn't want to be enabled all
115  * the time. LINVRNT() has to be explicitly enabled by --enable-invariants
116  * configure option.
117  */
118 # define LINVRNT(exp) LASSERT(exp)
119 #else
120 # define LINVRNT(exp) ((void)sizeof!!(exp))
121 #endif
122
123 #define KLASSERT(e) LASSERT(e)
124
125 void lbug_with_loc(struct libcfs_debug_msg_data *) __attribute__((noreturn));
126
127 #define LBUG()                                                          \
128 do {                                                                    \
129         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL);             \
130         lbug_with_loc(&msgdata);                                        \
131 } while(0)
132
133 extern cfs_atomic_t libcfs_kmemory;
134 /*
135  * Memory
136  */
137 #ifdef LIBCFS_DEBUG
138
139 # define libcfs_kmem_inc(ptr, size)             \
140 do {                                            \
141         cfs_atomic_add(size, &libcfs_kmemory);  \
142 } while (0)
143
144 # define libcfs_kmem_dec(ptr, size) do {        \
145         cfs_atomic_sub(size, &libcfs_kmemory);  \
146 } while (0)
147
148 #else
149 # define libcfs_kmem_inc(ptr, size) do {} while (0)
150 # define libcfs_kmem_dec(ptr, size) do {} while (0)
151 #endif /* LIBCFS_DEBUG */
152
153 #ifndef LIBCFS_VMALLOC_SIZE
154 #define LIBCFS_VMALLOC_SIZE        (2 << CFS_PAGE_SHIFT) /* 2 pages */
155 #endif
156
157 #define LIBCFS_ALLOC_GFP(ptr, size, mask)                                 \
158 do {                                                                      \
159         LASSERT(!cfs_in_interrupt() ||                                    \
160                (size <= LIBCFS_VMALLOC_SIZE && mask == CFS_ALLOC_ATOMIC));\
161         if (unlikely((size) > LIBCFS_VMALLOC_SIZE))                       \
162                 (ptr) = cfs_alloc_large(size);                            \
163         else                                                              \
164                 (ptr) = cfs_alloc((size), (mask));                        \
165         if (unlikely((ptr) == NULL)) {                                    \
166                 CERROR("LNET: out of memory at %s:%d (tried to alloc '"   \
167                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
168                 CERROR("LNET: %d total bytes allocated by lnet\n",        \
169                        cfs_atomic_read(&libcfs_kmemory));                 \
170                 break;                                                    \
171         }                                                                 \
172         libcfs_kmem_inc((ptr), (size));                                   \
173         memset((ptr), 0, (size));                                         \
174         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n",    \
175                (int)(size), (ptr), cfs_atomic_read (&libcfs_kmemory));    \
176 } while (0)
177
178 #define LIBCFS_ALLOC(ptr, size) \
179         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
180
181 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
182         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
183
184 #define LIBCFS_FREE(ptr, size)                                          \
185 do {                                                                    \
186         int s = (size);                                                 \
187         if (unlikely((ptr) == NULL)) {                                  \
188                 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
189                        "%s:%d\n", s, __FILE__, __LINE__);               \
190                 break;                                                  \
191         }                                                               \
192         libcfs_kmem_dec((ptr), s);                                      \
193         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
194                s, (ptr), cfs_atomic_read(&libcfs_kmemory));             \
195         if (unlikely(s > LIBCFS_VMALLOC_SIZE))                          \
196                 cfs_free_large(ptr);                                    \
197         else                                                            \
198                 cfs_free(ptr);                                          \
199 } while (0)
200
201 /******************************************************************************/
202
203 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
204 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
205 #define ___htonl(x) __cpu_to_be32(x)
206 #define ___htons(x) __cpu_to_be16(x)
207 #define ___ntohl(x) __be32_to_cpu(x)
208 #define ___ntohs(x) __be16_to_cpu(x)
209 #define htonl(x) ___htonl(x)
210 #define ntohl(x) ___ntohl(x)
211 #define htons(x) ___htons(x)
212 #define ntohs(x) ___ntohs(x)
213 #endif
214
215 void libcfs_debug_dumpstack(cfs_task_t *tsk);
216 void libcfs_run_upcall(char **argv);
217 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *);
218 void libcfs_debug_dumplog(void);
219 int libcfs_debug_init(unsigned long bufsize);
220 int libcfs_debug_cleanup(void);
221 int libcfs_debug_clear_buffer(void);
222 int libcfs_debug_mark_buffer(const char *text);
223
224 void libcfs_debug_set_level(unsigned int debug_level);
225
226 #else  /* !__KERNEL__ */
227 # ifdef LIBCFS_DEBUG
228 #  undef NDEBUG
229 #  include <assert.h>
230 #  define LASSERT(e)     assert(e)
231 #  define LASSERTF(cond, ...)                                                  \
232 do {                                                                           \
233           if (!(cond))                                                         \
234                 CERROR(__VA_ARGS__);                                           \
235           assert(cond);                                                        \
236 } while (0)
237 #  define LBUG()   assert(0)
238 #  ifdef INVARIANT_CHECK
239 #   define LINVRNT(exp) LASSERT(exp)
240 #  else
241 #   define LINVRNT(exp) ((void)sizeof!!(exp))
242 #  endif
243 # else
244 #  define LASSERT(e) ((void)sizeof!!(e))
245 #  define LASSERTF(cond, ...) ((void)sizeof!!(cond))
246 #  define LBUG()   ((void)(0))
247 #  define LINVRNT(exp) ((void)sizeof!!(exp))
248 # endif /* LIBCFS_DEBUG */
249 # define KLASSERT(e) ((void)0)
250 # define printk printf
251 # ifdef CRAY_XT3                                /* buggy calloc! */
252 #  define LIBCFS_ALLOC(ptr, size)               \
253    do {                                         \
254         (ptr) = malloc(size);                   \
255         memset(ptr, 0, size);                   \
256    } while (0)
257 # else
258 #  define LIBCFS_ALLOC(ptr, size) do { (ptr) = calloc(1,size); } while (0)
259 # endif
260 # define LIBCFS_FREE(ptr, size) do { free(ptr); } while((size) - (size))
261
262 void libcfs_debug_dumplog(void);
263 int libcfs_debug_init(unsigned long bufsize);
264 int libcfs_debug_cleanup(void);
265
266 #define libcfs_debug_dumpstack(tsk)     ((void)0)
267
268 /*
269  * Generic compiler-dependent macros required for kernel
270  * build go below this comment. Actual compiler/compiler version
271  * specific implementations come from the above header files
272  */
273 #ifdef __GNUC__
274 #define likely(x)      __builtin_expect(!!(x), 1)
275 #define unlikely(x)    __builtin_expect(!!(x), 0)
276 #else
277 #define likely(x)       (!!(x))
278 #define unlikely(x)     (!!(x))
279 #endif
280 /* !__KERNEL__ */
281 #endif
282
283 #define LASSERT_ATOMIC_ENABLED          (1)
284
285 #if LASSERT_ATOMIC_ENABLED
286
287 /** assert value of @a is equal to @v */
288 #define LASSERT_ATOMIC_EQ(a, v)                                 \
289 do {                                                            \
290         LASSERTF(cfs_atomic_read(a) == v,                       \
291                  "value: %d\n", cfs_atomic_read((a)));          \
292 } while (0)
293
294 /** assert value of @a is unequal to @v */
295 #define LASSERT_ATOMIC_NE(a, v)                                 \
296 do {                                                            \
297         LASSERTF(cfs_atomic_read(a) != v,                       \
298                  "value: %d\n", cfs_atomic_read((a)));          \
299 } while (0)
300
301 /** assert value of @a is little than @v */
302 #define LASSERT_ATOMIC_LT(a, v)                                 \
303 do {                                                            \
304         LASSERTF(cfs_atomic_read(a) < v,                        \
305                  "value: %d\n", cfs_atomic_read((a)));          \
306 } while (0)
307
308 /** assert value of @a is little/equal to @v */
309 #define LASSERT_ATOMIC_LE(a, v)                                 \
310 do {                                                            \
311         LASSERTF(cfs_atomic_read(a) <= v,                       \
312                  "value: %d\n", cfs_atomic_read((a)));          \
313 } while (0)
314
315 /** assert value of @a is great than @v */
316 #define LASSERT_ATOMIC_GT(a, v)                                 \
317 do {                                                            \
318         LASSERTF(cfs_atomic_read(a) > v,                        \
319                  "value: %d\n", cfs_atomic_read((a)));          \
320 } while (0)
321
322 /** assert value of @a is great/equal to @v */
323 #define LASSERT_ATOMIC_GE(a, v)                                 \
324 do {                                                            \
325         LASSERTF(cfs_atomic_read(a) >= v,                       \
326                  "value: %d\n", cfs_atomic_read((a)));          \
327 } while (0)
328
329 /** assert value of @a is great than @v1 and little than @v2 */
330 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)                         \
331 do {                                                            \
332         int __v = cfs_atomic_read(a);                           \
333         LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v);     \
334 } while (0)
335
336 /** assert value of @a is great than @v1 and little/equal to @v2 */
337 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)                         \
338 do {                                                            \
339         int __v = cfs_atomic_read(a);                           \
340         LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v);    \
341 } while (0)
342
343 /** assert value of @a is great/equal to @v1 and little than @v2 */
344 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)                         \
345 do {                                                            \
346         int __v = cfs_atomic_read(a);                           \
347         LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v);    \
348 } while (0)
349
350 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
351 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)                         \
352 do {                                                            \
353         int __v = cfs_atomic_read(a);                           \
354         LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v);   \
355 } while (0)
356
357 #else /* !LASSERT_ATOMIC_ENABLED */
358
359 #define LASSERT_ATOMIC_EQ(a, v)                 do {} while (0)
360 #define LASSERT_ATOMIC_NE(a, v)                 do {} while (0)
361 #define LASSERT_ATOMIC_LT(a, v)                 do {} while (0)
362 #define LASSERT_ATOMIC_LE(a, v)                 do {} while (0)
363 #define LASSERT_ATOMIC_GT(a, v)                 do {} while (0)
364 #define LASSERT_ATOMIC_GE(a, v)                 do {} while (0)
365 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)         do {} while (0)
366 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)         do {} while (0)
367 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)         do {} while (0)
368 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)         do {} while (0)
369
370 #endif /* LASSERT_ATOMIC_ENABLED */
371
372 #define LASSERT_ATOMIC_ZERO(a)                  LASSERT_ATOMIC_EQ(a, 0)
373 #define LASSERT_ATOMIC_POS(a)                   LASSERT_ATOMIC_GT(a, 0)
374
375 #define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
376 #define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof (*(ptr)));
377
378 /** Compile-time assertion.
379
380  * Check an invariant described by a constant expression at compile time by
381  * forcing a compiler error if it does not hold.  \a cond must be a constant
382  * expression as defined by the ISO C Standard:
383  *
384  *       6.8.4.2  The switch statement
385  *       ....
386  *       [#3] The expression of each case label shall be  an  integer
387  *       constant   expression  and  no  two  of  the  case  constant
388  *       expressions in the same switch statement shall have the same
389  *       value  after  conversion...
390  *
391  */
392 #define CLASSERT(cond) do {switch(42) {case (cond): case 0: break;}} while (0)
393
394 /* support decl needed both by kernel and liblustre */
395 int         libcfs_isknown_lnd(int type);
396 char       *libcfs_lnd2modname(int type);
397 char       *libcfs_lnd2str(int type);
398 int         libcfs_str2lnd(const char *str);
399 char       *libcfs_net2str(__u32 net);
400 char       *libcfs_nid2str(lnet_nid_t nid);
401 __u32       libcfs_str2net(const char *str);
402 lnet_nid_t  libcfs_str2nid(const char *str);
403 int         libcfs_str2anynid(lnet_nid_t *nid, const char *str);
404 char       *libcfs_id2str(lnet_process_id_t id);
405 int         cfs_iswhite(char c);
406 void        cfs_free_nidlist(cfs_list_t *list);
407 int         cfs_parse_nidlist(char *str, int len, cfs_list_t *list);
408 int         cfs_match_nid(lnet_nid_t nid, cfs_list_t *list);
409
410 /** \addtogroup lnet_addr
411  * @{ */
412 /* how an LNET NID encodes net:address */
413 /** extract the address part of an lnet_nid_t */
414 #define LNET_NIDADDR(nid)      ((__u32)((nid) & 0xffffffff))
415 /** extract the network part of an lnet_nid_t */
416 #define LNET_NIDNET(nid)       ((__u32)(((nid) >> 32)) & 0xffffffff)
417 /** make an lnet_nid_t from a network part and an address part */
418 #define LNET_MKNID(net,addr)   ((((__u64)(net))<<32)|((__u64)(addr)))
419 /* how net encodes type:number */
420 #define LNET_NETNUM(net)       ((net) & 0xffff)
421 #define LNET_NETTYP(net)       (((net) >> 16) & 0xffff)
422 #define LNET_MKNET(typ,num)    ((((__u32)(typ))<<16)|((__u32)(num)))
423 /** @} lnet_addr */
424
425 /* max value for numeric network address */
426 #define MAX_NUMERIC_VALUE 0xffffffff
427
428 /* implication */
429 #define ergo(a, b) (!(a) || (b))
430 /* logical equivalence */
431 #define equi(a, b) (!!(a) == !!(b))
432
433 #ifndef CFS_CURRENT_TIME
434 # define CFS_CURRENT_TIME time(0)
435 #endif
436
437 /* --------------------------------------------------------------------
438  * Light-weight trace
439  * Support for temporary event tracing with minimal Heisenberg effect.
440  * All stuff about lwt are put in arch/kp30.h
441  * -------------------------------------------------------------------- */
442
443 struct libcfs_device_userstate
444 {
445         int           ldu_memhog_pages;
446         cfs_page_t   *ldu_memhog_root_page;
447 };
448
449 /* what used to be in portals_lib.h */
450 #ifndef MIN
451 # define MIN(a,b) (((a)<(b)) ? (a): (b))
452 #endif
453 #ifndef MAX
454 # define MAX(a,b) (((a)>(b)) ? (a): (b))
455 #endif
456
457 #define MKSTR(ptr) ((ptr))? (ptr) : ""
458
459 static inline int cfs_size_round4 (int val)
460 {
461         return (val + 3) & (~0x3);
462 }
463
464 #ifndef HAVE_CFS_SIZE_ROUND
465 static inline int cfs_size_round (int val)
466 {
467         return (val + 7) & (~0x7);
468 }
469 #define HAVE_CFS_SIZE_ROUND
470 #endif
471
472 static inline int cfs_size_round16(int val)
473 {
474         return (val + 0xf) & (~0xf);
475 }
476
477 static inline int cfs_size_round32(int val)
478 {
479         return (val + 0x1f) & (~0x1f);
480 }
481
482 static inline int cfs_size_round0(int val)
483 {
484         if (!val)
485                 return 0;
486         return (val + 1 + 7) & (~0x7);
487 }
488
489 static inline size_t cfs_round_strlen(char *fset)
490 {
491         return (size_t)cfs_size_round((int)strlen(fset) + 1);
492 }
493
494 #define LOGL(var,len,ptr)                                       \
495 do {                                                            \
496         if (var)                                                \
497                 memcpy((char *)ptr, (const char *)var, len);    \
498         ptr += cfs_size_round(len);                             \
499 } while (0)
500
501 #define LOGU(var,len,ptr)                                       \
502 do {                                                            \
503         if (var)                                                \
504                 memcpy((char *)var, (const char *)ptr, len);    \
505         ptr += cfs_size_round(len);                             \
506 } while (0)
507
508 #define LOGL0(var,len,ptr)                              \
509 do {                                                    \
510         if (!len)                                       \
511                 break;                                  \
512         memcpy((char *)ptr, (const char *)var, len);    \
513         *((char *)(ptr) + len) = 0;                     \
514         ptr += cfs_size_round(len + 1);                 \
515 } while (0)
516
517 /**
518  *  Lustre Network Driver types.
519  */
520 enum {
521         /* Only add to these values (i.e. don't ever change or redefine them):
522          * network addresses depend on them... */
523         QSWLND    = 1,
524         SOCKLND   = 2,
525         GMLND     = 3, /* obsolete, keep it so that libcfs_nid2str works */
526         PTLLND    = 4,
527         O2IBLND   = 5,
528         CIBLND    = 6,
529         OPENIBLND = 7,
530         IIBLND    = 8,
531         LOLND     = 9,
532         RALND     = 10,
533         VIBLND    = 11,
534         MXLND     = 12,
535         GNILND    = 13,
536 };
537
538 #endif