From patchwork Tue May 8 13:40:24 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Teresa Johnson X-Patchwork-Id: 157682 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 804CDB6FA1 for ; Tue, 8 May 2012 23:40:51 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1337089251; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Received:Received:To:Subject:Message-Id:Date: From:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=up/jaJx CBDFW0XtNo9ups8buFtw=; b=enkPlAs2IPxalJ6sUageesOF16h1ye2wzY8YPcB /hI04/JMW4Bv+4njsHQUTXzU0N1kXuEN505TqLUdsLjzQIp/lLkq3GFxS10BLhRW 4gjJ1kHHDyqaMMjOVY6H528xEBXHmvl2HSsXYYAPS224iF84NVHsaHi1sxYlDKoR JbaQ= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:Received:Received:Received:To:Subject:Message-Id:Date:From:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=d3+sRnCtbqRneh7u2+PuxJcHoFXjqimt68Y8P8RW2Sy4h2RJBatv9+gqjpFE+m N74zyZcBJRhbCJKF7QQRnUOlwE8u+Dns8kXw2IVELLmZbtiglLCAkN3AMKWble2E TIP/4DLniYEyB+rjdxciQo4wa7tuT+vZaEtrLlbF1EF2M=; Received: (qmail 22120 invoked by alias); 8 May 2012 13:40:44 -0000 Received: (qmail 21947 invoked by uid 22791); 8 May 2012 13:40:42 -0000 X-SWARE-Spam-Status: No, hits=-4.4 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, TW_CL, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-lpp01m010-f73.google.com (HELO mail-lpp01m010-f73.google.com) (209.85.215.73) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 08 May 2012 13:40:28 +0000 Received: by laah2 with SMTP id h2so334375laa.2 for ; Tue, 08 May 2012 06:40:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=to:subject:message-id:date:from:x-gm-message-state; bh=FP+8+xOpY+CtfrXk4PDXvuPge88MGRI7OnOVr5N1NPc=; b=ca9B7O5nUHKUXW9u4J1GFTdMRsBNl/AKV731G2vSRWTiGKAms7n36ISgc7CquLxe25 yqE5U31E8OyBrbaejZVP9EA5vt6j9ehM0/DSVkQCrN2r2phnN2Q8FOyRUeDH3ylTodyI FuRYS62arfGwtqDWEVb09lLEfyRwoCTEABygHT9TcSNBWjUNRMbkkTCNxxnZ54y+hosi mpwzKiq9/boc5Dkc20G5zx/cMJIbERlH5/qdPha3nlV6Qg4wrnthXpgMSb8lNpvMtjQT J3ik5KP96XlQbfen76dM2YHZ1MvE7aWCyM2LT7TCuoFfIANKWrgov/x73BFbfS/s0yBS s3Bg== Received: by 10.14.29.2 with SMTP id h2mr1418770eea.5.1336484426215; Tue, 08 May 2012 06:40:26 -0700 (PDT) Received: by 10.14.29.2 with SMTP id h2mr1418762eea.5.1336484426097; Tue, 08 May 2012 06:40:26 -0700 (PDT) Received: from hpza9.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id t52si3802370eeb.0.2012.05.08.06.40.26 (version=TLSv1/SSLv3 cipher=AES128-SHA); Tue, 08 May 2012 06:40:26 -0700 (PDT) Received: from tjsboxrox.mtv.corp.google.com (tjsboxrox.mtv.corp.google.com [172.18.110.68]) by hpza9.eem.corp.google.com (Postfix) with ESMTP id B6D6D5C0050; Tue, 8 May 2012 06:40:25 -0700 (PDT) Received: by tjsboxrox.mtv.corp.google.com (Postfix, from userid 147431) id 82C1F61599; Tue, 8 May 2012 06:40:24 -0700 (PDT) To: reply@codereview.appspotmail.com,gcc-patches@gcc.gnu.org Subject: [PATCH] libgcov support for profile collection in region of interest (issue6186044) Message-Id: <20120508134024.82C1F61599@tjsboxrox.mtv.corp.google.com> Date: Tue, 8 May 2012 06:40:24 -0700 (PDT) From: tejohnson@google.com (Teresa Johnson) X-Gm-Message-State: ALoCoQl/fzDjFj/B523SBXFFm9E9s4ymXx9fUBq6h2zP/HlKdmihGxJZIDQJ8VuKIqjfUrKrCKtoSp+wEvmfryqNvhVtjoIHvfYAMc/y97Leo7/9R5JwsLz+TREdTUuTxQnO2BbZfgNp7wmUQIlpFfWiNj9jl9sTIiTyLrNCC9a9kqTfVUCikCM= X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi Honza, I added L_gcov_reset and L_gcov_dump for the new interfaces, and also added a description into the gcov man page. Let me know if it looks ok now. Bootstrapped and tested on x86_64-unknown-linux-gnu. Thanks, Teresa 2012-05-08 Teresa Johnson * libgcc/libgcov.c (gcov_clear, __gcov_reset): New functions. (__gcov_dump): Ditto. (gcov_dump_complete): New global variable. (__gcov_flush): Outline functionality now in gcov_clear. * gcc/gcov-io.h (__gcov_reset, __gcov_dump): Declare. * libgcc/Makefile.in (L_gcov_reset, L_gcov_dump): Define. * gcc/doc/gcov.texi: Add note on using __gcov_reset and __gcov_dump. --- This patch is available for review at http://codereview.appspot.com/6186044 Index: libgcc/Makefile.in =================================================================== --- libgcc/Makefile.in (revision 187048) +++ libgcc/Makefile.in (working copy) @@ -849,7 +849,7 @@ include $(iterator) # Defined in libgcov.c, included only in gcov library LIBGCOV = _gcov _gcov_merge_add _gcov_merge_single _gcov_merge_delta \ _gcov_fork _gcov_execl _gcov_execlp _gcov_execle \ - _gcov_execv _gcov_execvp _gcov_execve \ + _gcov_execv _gcov_execvp _gcov_execve _gcov_reset _gcov_dump \ _gcov_interval_profiler _gcov_pow2_profiler _gcov_one_value_profiler \ _gcov_indirect_call_profiler _gcov_average_profiler _gcov_ior_profiler \ _gcov_merge_ior Index: libgcc/libgcov.c =================================================================== --- libgcc/libgcov.c (revision 187048) +++ libgcc/libgcov.c (working copy) @@ -50,6 +50,14 @@ void __gcov_init (struct gcov_info *p __attribute_ void __gcov_flush (void) {} #endif +#ifdef L_gcov_reset +void __gcov_reset (void) {} +#endif + +#ifdef L_gcov_dump +void __gcov_dump (void) {} +#endif + #ifdef L_gcov_merge_add void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)), unsigned n_counters __attribute__ ((unused))) {} @@ -74,7 +82,7 @@ void __gcov_merge_delta (gcov_type *counters __at #include #endif -#ifdef L_gcov +#if defined(L_gcov) || defined(L_gcov_reset) || defined(L_gcov_dump) #include "gcov-io.c" struct gcov_fn_buffer @@ -91,6 +99,9 @@ static struct gcov_info *gcov_list; /* Size of the longest file name. */ static size_t gcov_max_filename = 0; +/* Flag when the profile has already been dumped via __gcov_dump(). */ +static int gcov_dump_complete = 0; + /* Make sure path component of the given FILENAME exists, create missing directories. FILENAME must be writable. Returns zero on success, or -1 if an error occurred. */ @@ -286,6 +297,11 @@ gcov_exit (void) char *gi_filename, *gi_filename_up; gcov_unsigned_t crc32 = 0; + /* Prevent the counters from being dumped a second time on exit when the + application already wrote out the profile using __gcov_dump(). */ + if (gcov_dump_complete) + return; + memset (&all_prg, 0, sizeof (all_prg)); /* Find the totals for this execution. */ memset (&this_prg, 0, sizeof (this_prg)); @@ -679,6 +695,37 @@ gcov_exit (void) } } +/* Reset all counters to zero. */ + +static void +gcov_clear (void) +{ + const struct gcov_info *gi_ptr; + + for (gi_ptr = gcov_list; gi_ptr; gi_ptr = gi_ptr->next) + { + unsigned f_ix; + + for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++) + { + unsigned t_ix; + const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix]; + + if (!gfi_ptr || gfi_ptr->key != gi_ptr) + continue; + const struct gcov_ctr_info *ci_ptr = gfi_ptr->ctrs; + for (t_ix = 0; t_ix != GCOV_COUNTERS; t_ix++) + { + if (!gi_ptr->merge[t_ix]) + continue; + + memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num); + ci_ptr++; + } + } + } +} + /* Add a new object file onto the bb chain. Invoked automatically when running an object file's global ctors. */ @@ -730,40 +777,48 @@ init_mx_once (void) void __gcov_flush (void) { - const struct gcov_info *gi_ptr; - init_mx_once (); __gthread_mutex_lock (&__gcov_flush_mx); gcov_exit (); - for (gi_ptr = gcov_list; gi_ptr; gi_ptr = gi_ptr->next) - { - unsigned f_ix; + gcov_clear (); - for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++) - { - unsigned t_ix; - const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix]; - - if (!gfi_ptr || gfi_ptr->key != gi_ptr) - continue; - const struct gcov_ctr_info *ci_ptr = gfi_ptr->ctrs; - for (t_ix = 0; t_ix != GCOV_COUNTERS; t_ix++) - { - if (!gi_ptr->merge[t_ix]) - continue; - - memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num); - ci_ptr++; - } - } - } - __gthread_mutex_unlock (&__gcov_flush_mx); } #endif /* L_gcov */ +#ifdef L_gcov_reset + +/* Function that can be called from application to reset counters to zero, + in order to collect profile in region of interest. */ + +void +__gcov_reset (void) +{ + gcov_clear (); + /* Re-enable dumping to support collecting profile in multiple regions + of interest. */ + gcov_dump_complete = 0; +} + +#endif /* L_gcov_reset */ + +#ifdef L_gcov_dump + +/* Function that can be called from application to write profile collected + so far, in order to collect profile in region of interest. */ + +void +__gcov_dump (void) +{ + gcov_exit (); + /* Prevent profile from being dumped a second time on application exit. */ + gcov_dump_complete = 1; +} + +#endif /* L_gcov_dump */ + #ifdef L_gcov_merge_add /* The profile merging function that just adds the counters. It is given an array COUNTERS of N_COUNTERS old counters and it reads the same number Index: gcc/doc/gcov.texi =================================================================== --- gcc/doc/gcov.texi (revision 187048) +++ gcc/doc/gcov.texi (working copy) @@ -538,6 +538,12 @@ now be calculable at compile time in some instance coverage of all the uses of the inline function will be shown for the same source lines, the line counts themselves might seem inconsistent. +Long-running applications can use the @code{_gcov_reset} and @code{_gcov_dump} +facilities to restrict profile collection to the program region of +interest. Calling @code{_gcov_reset(void)} will clear all profile counters +to zero, and calling @code{_gcov_dump(void)} will cause the profile information +collected at that point to be dumped to @file{.gcda} output files. + @c man end @node Gcov Data Files Index: gcc/gcov-io.h =================================================================== --- gcc/gcov-io.h (revision 187048) +++ gcc/gcov-io.h (working copy) @@ -458,6 +458,12 @@ extern void __gcov_init (struct gcov_info *) ATTRI /* Called before fork, to avoid double counting. */ extern void __gcov_flush (void) ATTRIBUTE_HIDDEN; +/* Function to reset all counters to 0. */ +extern void __gcov_reset (void); + +/* Function to enable early write of profile information so far. */ +extern void __gcov_dump (void); + /* The merge function that just sums the counters. */ extern void __gcov_merge_add (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;