From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_2 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7E89C4741F for ; Thu, 5 Nov 2020 18:17:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6ADF121734 for ; Thu, 5 Nov 2020 18:17:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729783AbgKESRi (ORCPT ); Thu, 5 Nov 2020 13:17:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:45124 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727851AbgKESRh (ORCPT ); Thu, 5 Nov 2020 13:17:37 -0500 Received: from gandalf.local.home (cpe-66-24-58-225.stny.res.rr.com [66.24.58.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3D8C72078E; Thu, 5 Nov 2020 18:17:37 +0000 (UTC) Date: Thu, 5 Nov 2020 13:17:35 -0500 From: Steven Rostedt To: "Yordan Karadzhov (VMware)" Cc: linux-trace-devel@vger.kernel.org Subject: Re: [PATCH v2 10/20] kernel-shark: Start using data streams Message-ID: <20201105131735.3d559bce@gandalf.local.home> In-Reply-To: References: <20201012133523.469040-1-y.karadz@gmail.com> <20201012133523.469040-11-y.karadz@gmail.com> <20201014145627.675e3c70@gandalf.local.home> X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org Archived-At: List-Archive: List-Post: On Thu, 5 Nov 2020 16:58:51 +0200 "Yordan Karadzhov (VMware)" wrote: > I see the problem. This is definitely wrong. > > What if in addition to "n_streams" I add another counter called > "last_stream_added" and initialize this counter to -1? > > Then I can add streams like this: > > kshark_ctx->stream[++kshark_ctx->last_stream_added] = stream; > ++kshark_ctx->n_streams; > > > You may want to do instead: I'm thinking of doing something like this: struct kshark_ctx { [..] unsigned int free_stream; [..] }; if (kshark_ctx->free_stream >= kshark_ctx->n_streams) { kshark_ctx->stream[++kshark_ctx->n_streams] = stream; kshark_ctx->free_stream = kshark_ctx->n_streams; /* BTW, the stream array should be allocated to the n_streams, and reallocated when it grows. I don't think we want a huge stream array to handle all the bits when not used. */ } else { int new_stream = kshark_ctx->free_stream; kshark_ctx->free_stream = kshark_index(kshark_ctx->stream[new_stream]); kshark_ctx->stream[new_stream] = stream; } For freeing (index i): kshark_ctx->stream[i] = kshark_ptr(kshark_ctx->free_stream); kshark_ctx->free_stream = i; We could define the following (note, I just used these names for the functions, they could be named something else): #define KSHARK_INDEX_MASK ((1 << NR_OF_BITS_FOR_STREAM) - 1) #define KSHARK_INVALID_STREAM (~((1UL << NR_OF_BITS_FOR_STREAM) - 1)) static inline int kshark_index(void *ptr) { unsigned long index = (unsigned long)ptr; return (int)(index & KSHARK_INDEX_MASK); } static inline void *kshark_ptr(unsigned int index) { unsigned long p; p = KSHARK_INVALID_STREAM | index; return (void *)p; } The KSHARK_INVALID_STREAM and KSHARK_INDEX_MASK, would allow us to do something like this if we wanted to loop through all streams: static inline bool kshark_is_valid_stream(void *ptr) { unsigned long p = (unsigned long)ptr; return (p & KSHARK_INVALID_STREAM) == KSHARK_INVALID_STREAM; } The above works because the address of setting all those bits, would put the address into the kernel space (illegal user space address). for (i = 0; i < kshark_ctx->n_streams; i++) { if (!kshark_is_valid_stream(kshark_ctx->stream[i])) continue; /* process valid stream */ } -- Steve