int ejsAddConstant(Ejs *ejs, EjsModule *mp, cchar *str) { EjsConstants *cp; ssize len, oldLen; int index; cp = mp->constants; if (cp->locked) { mprLog("ejs vm", 0, "Constant pool for module is locked. Cannot add constant \"%s\".", str); return MPR_ERR_CANT_WRITE; } lock(mp); len = slen(str) + 1; if (ejsGrowConstants(ejs, mp, len) < 0) { unlock(mp); return MPR_ERR_MEMORY; } memcpy(&cp->pool[cp->poolLength], str, len); oldLen = cp->poolLength; cp->poolLength += len; mprAddKey(cp->table, str, ITOP(cp->indexCount)); cp->index[cp->indexCount] = ITOP(oldLen << 1 | 1); index = cp->indexCount++; unlock(mp); return index; }
/* Parent may be null */ PUBLIC HttpTrace *httpCreateTrace(HttpTrace *parent) { HttpTrace *trace; if ((trace = mprAllocObj(HttpTrace, manageTrace)) == 0) { return 0; } if (parent) { *trace = *parent; trace->parent = parent; } else { if ((trace->events = mprCreateHash(0, MPR_HASH_STATIC_VALUES)) == 0) { return 0; } mprAddKey(trace->events, "request", ITOP(1)); mprAddKey(trace->events, "result", ITOP(2)); mprAddKey(trace->events, "error", ITOP(2)); mprAddKey(trace->events, "context", ITOP(3)); mprAddKey(trace->events, "form", ITOP(4)); mprAddKey(trace->events, "body", ITOP(5)); mprAddKey(trace->events, "debug", ITOP(5)); trace->size = HTTP_TRACE_MAX_SIZE; trace->formatter = httpDetailTraceFormatter; trace->logger = httpWriteTraceLogFile; trace->mutex = mprCreateLock(); } return trace; }
static int growArray(Ejs *ejs, EjsArray *ap, int len) { EjsObj **dp; ssize size, factor, count; int i; assert(ap); if (len <= 0) { return 0; } if (len <= ap->length) { return 0; } size = mprGetBlockSize(ap->data); size = (int) (mprGetBlockSize(ap->data) / sizeof(EjsObj*)); /* Allocate or grow the data structures. */ if (len > size) { if (size > EJS_LOTSA_PROP) { /* Looks like a big object so grow by a bigger chunk */ factor = max(size / 4, EJS_ROUND_PROP); count = (len + factor) / factor * factor; } else { count = len; } // OPT - this is currently 16 count = EJS_PROP_ROUNDUP(count); if (ap->data == 0) { assert(ap->length == 0); assert(count > 0); if ((ap->data = mprAllocZeroed(sizeof(EjsObj*) * count)) == 0) { return EJS_ERR; } } else { assert(size > 0); if ((ap->data = mprRealloc(ap->data, sizeof(EjsObj*) * count)) == 0) { return EJS_ERR; } } dp = &ap->data[ap->length]; for (i = ap->length; i < count; i++) { *dp++ = ESV(undefined); } } else { mprNop(ITOP(size)); } ap->length = len; return 0; }
PUBLIC void httpSetTraceFormatterName(HttpTrace *trace, cchar *name) { HttpTraceFormatter formatter; if (name && smatch(name, "common")) { if ((trace->events = mprCreateHash(0, MPR_HASH_STATIC_VALUES)) == 0) { return; } mprAddKey(trace->events, "complete", ITOP(0)); formatter = httpCommonTraceFormatter; } else { formatter = httpDetailTraceFormatter; } httpSetTraceFormatter(trace, formatter); }
static int vfs_vget_9p(struct mount *mp, ino64_t ino, vnode_t *vpp, vfs_context_t ctx) { vnode_t vp; qid_9p qid; int e; TRACE(); qid.path = ITOP(ino); qid.type = ITOT(ino); *vpp = NULL; if ((e=nget_9p(MTO9P(mp), NOFID, qid, NULL, &vp, NULL, ctx))) return e; nunlock_9p(NTO9P(vp)); if ((e=vnode_get(vp))) return e; *vpp = vp; return 0; }
static ssize_t lcdi2c_fopread(struct file *file, char __user *buffer, size_t length, loff_t *offset) { u8 i = 0; CRIT_BEG(data, EBUSY); if (data->devicefileptr == (data->organization.columns * data->organization.rows)) return 0; while(i < length && i < (data->organization.columns * data->organization.rows)) { put_user(data->buffer[i], buffer++); data->devicefileptr++; i++; } i %= (data->organization.columns * data->organization.rows); ITOP(data, i, data->column, data->row); lcdsetcursor(data, data->column, data->row); (*offset) = i; CRIT_END(data); return i; }
/* OPT */ bool httpValidateLimits(HttpEndpoint *endpoint, int event, HttpConn *conn) { HttpLimits *limits; Http *http; cchar *action; int count, level, dir; limits = conn->limits; dir = HTTP_TRACE_RX; action = "unknown"; mprAssert(conn->endpoint == endpoint); http = endpoint->http; lock(http); switch (event) { case HTTP_VALIDATE_OPEN_CONN: /* This measures active client systems with unique IP addresses. */ if (endpoint->clientCount >= limits->clientMax) { unlock(http); /* Abort connection */ httpError(conn, HTTP_ABORT | HTTP_CODE_SERVICE_UNAVAILABLE, "Too many concurrent clients %d/%d", endpoint->clientCount, limits->clientMax); return 0; } count = (int) PTOL(mprLookupKey(endpoint->clientLoad, conn->ip)); mprAddKey(endpoint->clientLoad, conn->ip, ITOP(count + 1)); endpoint->clientCount = (int) mprGetHashLength(endpoint->clientLoad); action = "open conn"; dir = HTTP_TRACE_RX; break; case HTTP_VALIDATE_CLOSE_CONN: count = (int) PTOL(mprLookupKey(endpoint->clientLoad, conn->ip)); if (count > 1) { mprAddKey(endpoint->clientLoad, conn->ip, ITOP(count - 1)); } else { mprRemoveKey(endpoint->clientLoad, conn->ip); } endpoint->clientCount = (int) mprGetHashLength(endpoint->clientLoad); action = "close conn"; dir = HTTP_TRACE_TX; break; case HTTP_VALIDATE_OPEN_REQUEST: mprAssert(conn->rx); if (endpoint->requestCount >= limits->requestMax) { unlock(http); httpError(conn, HTTP_CODE_SERVICE_UNAVAILABLE, "Server overloaded"); mprLog(2, "Too many concurrent requests %d/%d", endpoint->requestCount, limits->requestMax); return 0; } endpoint->requestCount++; conn->rx->flags |= HTTP_LIMITS_OPENED; action = "open request"; dir = HTTP_TRACE_RX; break; case HTTP_VALIDATE_CLOSE_REQUEST: if (conn->rx && conn->rx->flags & HTTP_LIMITS_OPENED) { /* Requests incremented only when conn->rx is assigned */ endpoint->requestCount--; mprAssert(endpoint->requestCount >= 0); action = "close request"; dir = HTTP_TRACE_TX; conn->rx->flags &= ~HTTP_LIMITS_OPENED; } break; case HTTP_VALIDATE_OPEN_PROCESS: if (http->processCount >= limits->processMax) { unlock(http); httpError(conn, HTTP_CODE_SERVICE_UNAVAILABLE, "Server overloaded"); mprLog(2, "Too many concurrent processes %d/%d", http->processCount, limits->processMax); return 0; } http->processCount++; action = "start process"; dir = HTTP_TRACE_RX; break; case HTTP_VALIDATE_CLOSE_PROCESS: http->processCount--; mprAssert(http->processCount >= 0); break; } if (event == HTTP_VALIDATE_CLOSE_CONN || event == HTTP_VALIDATE_CLOSE_REQUEST) { if ((level = httpShouldTrace(conn, dir, HTTP_TRACE_LIMITS, NULL)) >= 0) { LOG(4, "Validate request for %s. Active connections %d, active requests: %d/%d, active client IP %d/%d", action, mprGetListLength(http->connections), endpoint->requestCount, limits->requestMax, endpoint->clientCount, limits->clientMax); } } unlock(http); return 1; }
PUBLIC void httpSetTraceEventLevel(HttpTrace *trace, cchar *type, int level) { assert(trace); mprAddKey(trace->events, type, ITOP(level)); }
/* Do a performance benchmark */ static void doBenchmark(void *thread) { MprTime start; MprList *list; int count, i; MprMutex *lock; MprSpin *spin; mprPrintf("Group\t%-30s\t%13s\t%12s\n", "Benchmark", "Microsec", "Elapsed-sec"); testMalloc(); if (!app->testAllocOnly) { /* Locking primitives */ mprPrintf("Lock Benchmarks\n"); lock = mprCreateLock(); count = 5000000 * app->iterations; start = startMark(); for (i = 0; i < count; i++) { mprLock(lock); mprUnlock(lock); } endMark(start, count, "Mutex lock|unlock"); /* Locking primitives */ mprPrintf("Lock Benchmarks\n"); spin = mprCreateSpinLock(); count = 5000000 * app->iterations; start = startMark(); for (i = 0; i < count; i++) { mprSpinLock(spin); mprSpinUnlock(spin); } endMark(start, count, "Spin lock|unlock"); /* Condition signal / wait */ mprPrintf("Cond Benchmarks\n"); count = 1000000 * app->iterations; start = startMark(); mprResetCond(app->complete); for (i = 0; i < count; i++) { mprSignalCond(app->complete); mprWaitForCond(app->complete, -1); } endMark(start, count, "Cond signal|wait"); /* List */ mprPrintf("List Benchmarks\n"); count = 2000000 * app->iterations; list = mprCreateList(count, 0); start = startMark(); for (i = 0; i < count; i++) { mprAddItem(list, (void*) (long) i); mprRemoveItem(list, (void*) (long) i); } endMark(start, count, "Link insert|remove"); /* Events */ mprPrintf("Event Benchmarks\n"); mprResetCond(app->complete); count = 30000 * app->iterations; app->markCount = count; start = startMark(); for (i = 0; i < count; i++) { mprCreateEvent(NULL, "eventBenchmark", 0, eventCallback, ITOP(i), MPR_EVENT_QUICK); } mprWaitForCond(app->complete, -1); endMark(start, count, "Event (create|run|delete)"); /* Test timer creation, run and remove These create a new dispatcher and run a worker thread. */ mprPrintf("Timer\n"); mprResetCond(app->complete); count = 20000 * app->iterations; app->markCount = count; start = startMark(); for (i = 0; i < count; i++) { mprCreateTimerEvent(NULL, "timerBenchmark", 0, timerCallback, (void*) (long) i, 0); } mprWaitForCond(app->complete, -1); endMark(start, count, "Timer (create|delete)"); /* Alloc (1K) */ mprPrintf("Alloc 1K Benchmarks\n"); count = 2000000 * app->iterations; start = startMark(); for (i = 0; i < count; i++) { mprAlloc(1024); if ((i % 128) == 0) { mprGC(0); } } endMark(start, count, "Alloc mprAlloc(1K)"); } testComplete = 1; }