From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 2BD7B6ECC0; Wed, 13 Apr 2022 23:38:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2BD7B6ECC0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1649882339; bh=qDxkmDvUOK69IAWKGUL4dJ819vFYZLYwxy3n1t5kkek=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=urUZznwVMFe1dVhckAvao3FF1cz/wsWkXPvdpI+IKxTOn5LcZIuZj5n9L017UNIyB rrqpCnRM+Dv+KupIoN+hg1j5Dr1X83vNLWFPeKGyw7riM1M5kLsrNfX1a7IJciOAEv MRjEj5WnhbCs0+hMMJHhufNQR0pxMC2blMfEvnAc= Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com [209.85.208.172]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 1ECC86EFF4 for ; Wed, 13 Apr 2022 23:36:24 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1ECC86EFF4 Received: by mail-lj1-f172.google.com with SMTP id r18so3684281ljp.0 for ; Wed, 13 Apr 2022 13:36:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XKtAJSpeZycRCnAerrTv8GV9tEhZ/gtydSpwSEBv4Vg=; b=nzgFA+5pHg6yY2E7YWk0mtjpC1iqWscrNFToCpEHgHMv/NNBrpfWKmjGutrmamN1e2 0Gqt7XgdotPgCIXZrA/TwsDHuWPSA5alObj+AzUrvKK0xs06kLlbkWKcmzmDZLba0Wg8 X0x1BszTm/PllDOpZPhWP1kgGGfIw6atPYyFDUNHP0WrR143wpdwPwSGZC0YIYzApUee uhktQMR3XdTh8TzVQIA9p4PVJWvdDHyP7TXrxbq6Y9K4d07IJhyoFwxbwtF4fqkNwIXw H1p9JK50e2PewiITNa/IKdE9eIhJCX72HLWz6siT6e88pyAx8nYyyHbeG4QcxxCHCxYq z9kA== X-Gm-Message-State: AOAM5338A5LR/lPeKxHYF2LABl78C954zYrTiRWQROs39E53klGl5LpM K3848A3Vn8XmH7S4tKDFYz383pP9d9AqhQ== X-Google-Smtp-Source: ABdhPJyXdT2UhH7FAQspP38N/rOUSWXfTWIlMgd97j3TDzkPviaSL/R/ygPa8/WTmIDI/ZzJx+JydA== X-Received: by 2002:a2e:a914:0:b0:24b:f64:c26f with SMTP id j20-20020a2ea914000000b0024b0f64c26fmr27393071ljq.203.1649882183338; Wed, 13 Apr 2022 13:36:23 -0700 (PDT) Received: from localhost.localdomain ([93.175.11.199]) by smtp.gmail.com with ESMTPSA id e15-20020a19500f000000b0046bb76678bcsm2389lfb.131.2022.04.13.13.36.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 13:36:22 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Wed, 13 Apr 2022 23:36:11 +0300 Message-Id: X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v4 5/7] memprof: add profile common section X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" A Lua API for sysprof needs to be introduced, but sysprof's C API is quite similar with memprof's. Considering this, there are some structures and functions that should be common among memprof's and sysprof's implementations of Lua API to avoid duplication. Part of tarantool/tarantool#781 --- src/lib_misc.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/lib_misc.c b/src/lib_misc.c index 1dab08cc..22d29d78 100644 --- a/src/lib_misc.c +++ b/src/lib_misc.c @@ -8,8 +8,9 @@ #define lib_misc_c #define LUA_LIB -#include #include +#include +#include #include "lua.h" #include "lmisclib.h" @@ -75,9 +76,7 @@ LJLIB_CF(misc_getmetrics) #include "lj_libdef.h" -/* ----- misc.memprof module ---------------------------------------------- */ - -#define LJLIB_MODULE_misc_memprof +/* --------- profile common section --------------------------------------- */ /* ** Yep, 8Mb. Tuned in order not to bother the platform with too often flushes. @@ -85,9 +84,9 @@ LJLIB_CF(misc_getmetrics) #define STREAM_BUFFER_SIZE (8 * 1024 * 1024) /* Structure given as ctx to memprof writer and on_stop callback. */ -struct memprof_ctx { - /* Output file stream for data. */ - FILE *stream; +struct profile_ctx { + /* Output file descriptor for data. */ + int fd; /* Profiled global_State for lj_mem_free at on_stop callback. */ global_State *g; /* Buffer for data. */ @@ -96,13 +95,13 @@ struct memprof_ctx { /* ** Default buffer writer function. -** Just call fwrite to the corresponding FILE. +** Just call write to the corresponding descriptor. */ static size_t buffer_writer_default(const void **buf_addr, size_t len, void *opt) { - struct memprof_ctx *ctx = opt; - FILE *stream = ctx->stream; + struct profile_ctx *ctx = opt; + const int fd = ctx->fd; const void * const buf_start = *buf_addr; const void *data = *buf_addr; size_t write_total = 0; @@ -110,9 +109,9 @@ static size_t buffer_writer_default(const void **buf_addr, size_t len, lua_assert(len <= STREAM_BUFFER_SIZE); for (;;) { - const size_t written = fwrite(data, 1, len - write_total, stream); + const size_t written = write(fd, data, len - write_total); - if (LJ_UNLIKELY(written == 0)) { + if (LJ_UNLIKELY(written == -1)) { /* Re-tries write in case of EINTR. */ if (errno != EINTR) { /* Will be freed as whole chunk later. */ @@ -137,22 +136,25 @@ static size_t buffer_writer_default(const void **buf_addr, size_t len, return write_total; } -/* Default on stop callback. Just close the corresponding stream. */ +/* Default on stop callback. Just close the corresponding descriptor. */ static int on_stop_cb_default(void *opt, uint8_t *buf) { - struct memprof_ctx *ctx = opt; - FILE *stream = ctx->stream; + struct profile_ctx *ctx = opt; + const int fd = ctx->fd; UNUSED(buf); lj_mem_free(ctx->g, ctx, sizeof(*ctx)); - return fclose(stream); + return close(fd); } +/* ----- misc.memprof module ---------------------------------------------- */ + +#define LJLIB_MODULE_misc_memprof /* local started, err, errno = misc.memprof.start(fname) */ LJLIB_CF(misc_memprof_start) { struct lj_memprof_options opt = {0}; const char *fname = strdata(lj_lib_checkstr(L, 1)); - struct memprof_ctx *ctx; + struct profile_ctx *ctx; int memprof_status; /* -- 2.35.1