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 79AB96ECC0; Wed, 20 Apr 2022 16:00:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 79AB96ECC0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1650459647; bh=do3XfAYAsrGFI8RfBz7d41qNR8TCxPPcla379iY/kC0=; 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=sMV+febqTrniJgr7BD7sfqF5DvAQTtGGABEic+6413rcImkiHaAyB0aX8towR+YIh 4mjKVlYVZN71Q6l1vZwoNS+aWJcyHJe1ugSthSp0XFkoomRGFXU4MQh/vG6LbF78lu p+VPXZXvaQaoGyBvUYlWV8PmxBT0wji0HmDLHfmk= Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (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 4B5116ECC0 for ; Wed, 20 Apr 2022 15:58:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4B5116ECC0 Received: by mail-lf1-f52.google.com with SMTP id bu29so2892934lfb.0 for ; Wed, 20 Apr 2022 05:58:18 -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=Tz4UALcVdTSC03M6ZzI5pFPDZ9jG4ugt36rs0/LfWxc=; b=fx8L6bqwjtLLESf15HcQbcN+HtYHMdZ8Q9vxJy1qq0OLP7yKS9ou8tL5U8U+NMe8rY jwORtbS/Bezw/BI1NRh+ZkFoY47UWn+/xaeU0PumurW577MiybnhgkM5gxCTqtYOABR3 IMNPc+5rTruqlgujC4+WWbs+OI0OurrA0qfWRrpg+IoBRI17qgm5yEEl1lq4z8w80MDa tBavE/0OqGIKWwusU56UGj7NEa+Uz+GHftAxRaP2vjAf7JLChDh3MlBGrs//XQyDAu/w 6sAFPXzXwZm7tZX3+RWd7HD6CCy3H4y38FPCJ2yam2mbRcBahH+s3Km0ICpoYV4eg9gl lCUg== X-Gm-Message-State: AOAM532Dx89G/TV4vLU+lLIhat8LotioY6frPw1VGuZZ4Doax/iLPgYX sxXVVMjyqul2wXo9VxQZMIa6iB8WaL9aBA== X-Google-Smtp-Source: ABdhPJye0JShhpRq9w0bGbpe23iQsrZy7zmLL07tkOXwZuhMPMnW9BhP3WzdBJQ1VgJ72ZaB4O8SPg== X-Received: by 2002:ac2:4573:0:b0:46d:7f8:6c2b with SMTP id k19-20020ac24573000000b0046d07f86c2bmr14439269lfm.265.1650459497687; Wed, 20 Apr 2022 05:58:17 -0700 (PDT) Received: from localhost.localdomain ([93.175.28.57]) by smtp.gmail.com with ESMTPSA id o3-20020a198c03000000b00448b7b1780csm1824076lfd.63.2022.04.20.05.58.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 05:58:17 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Wed, 20 Apr 2022 15:58:01 +0300 Message-Id: <74776d86ec9928d2cd4323f277d684ea2fc8b46a.1650459246.git.m.kokryashkin@tarantool.org> 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 v5 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 | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/lib_misc.c b/src/lib_misc.c index 1dab08cc..bfeddc6a 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; /* @@ -167,9 +169,9 @@ LJLIB_CF(misc_memprof_start) opt.len = STREAM_BUFFER_SIZE; ctx->g = G(L); - ctx->stream = fopen(fname, "wb"); + ctx->fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, 0644); - if (ctx->stream == NULL) { + if (ctx->fd == -1) { lj_mem_free(ctx->g, ctx, sizeof(*ctx)); return luaL_fileresult(L, 0, fname); } -- 2.35.1