From 9bea1a199d820f7a1f54343cdfc613cc66b4c2cb Mon Sep 17 00:00:00 2001 From: Igor Wodiany Date: Wed, 6 Nov 2019 17:14:01 +0000 Subject: [PATCH] Add initial version of the code The initial release of AfterOMPT implements all callbacks available in LLVM 9.0 and allows capturing some of information related to them as an Aftermath trace. --- .gitignore | 2 + CMakeLists.txt | 28 +++ LICENSE | 502 ++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 169 ++++++++++++++++ src/afterompt.c | 466 ++++++++++++++++++++++++++++++++++++++++++++ src/afterompt.h | 112 +++++++++++ src/trace.c | 335 ++++++++++++++++++++++++++++++++ src/trace.h | 68 +++++++ 8 files changed, 1682 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/afterompt.c create mode 100644 src/afterompt.h create mode 100644 src/trace.c create mode 100644 src/trace.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc28c5a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +install/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bd98368 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.10) + +project(afterompt VERSION 0.1.0 DESCRIPTION "OMPT tool for generating Aftermath traces") + +find_package(PkgConfig) + +pkg_check_modules(LIBTRACE REQUIRED libaftermath-trace) + +set(CMAKE_C_FLAGS_DEBUG "-g -fopenmp") +set(CMAKE_C_FLAGS_RELEASE "-fopenmp") + +set(SOURCES + "src/afterompt.c" + "src/trace.c" +) + +link_directories(${LIBTRACE_LIBRARY_DIRS}) + +add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCES}) + +set_property(TARGET ${CMAKE_PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON) + +target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${LIBTRACE_INCLUDE_DIRS}) + +target_link_libraries(${CMAKE_PROJECT_NAME} ${LIBTRACE_LIBRARIES}) + +install(TARGETS ${CMAKE_PROJECT_NAME} DESTINATION ${PROJECT_SOURCE_DIR}/install) + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4362b49 --- /dev/null +++ b/LICENSE @@ -0,0 +1,502 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/README.md b/README.md new file mode 100644 index 0000000..d5cbf47 --- /dev/null +++ b/README.md @@ -0,0 +1,169 @@ +# AfterOMPT + +OMPT tool for generating [Aftermath](https://www.aftermath-tracing.com/) traces. + +## Overview + +AfterOMPT is the OpenMP first-party tool that implements OMPT callbacks +in order to track OpenMP events and to access the runtime state of the +system. We use Aftermath tracing library to collect information about +those events and save them to the trace file that can be later viewed +using Aftermath GUI. Detailed information about OMPT can be found +in the [OpenMP standard](https://www.openmp.org/specifications/) + +## Available tracing information + +Currently the following information is traced and saved on the +disk: + +``` +am_dsk_openmp_thread, +am_dsk_openmp_parallel, +am_dsk_openmp_task_create, +am_dsk_openmp_task_schedule, +am_dsk_openmp_implicit_task, +am_dsk_openmp_sync_region_wait, +am_dsk_openmp_mutex_released, +am_dsk_openmp_dependences, +am_dsk_openmp_task_dependence, +am_dsk_openmp_work, +am_dsk_openmp_master, +am_dsk_openmp_sync_region, +am_dsk_openmp_lock_init, +am_dsk_openmp_lock_destroy, +am_dsk_openmp_mutex_acquire, +am_dsk_openmp_mutex_acquired, +am_dsk_openmp_nest_lock, +am_dsk_openmp_flush, +am_dsk_openmp_cancel +``` + +Data above has corresponding in-memory representation in Aftermath that +can be used to visualize and analyse the data. Please refer to Aftermath +on disk data types definitions for detailed description of each state. + +## Callbacks status + +To produced described tracing information the following +callbacks were implemented: + +* `ompt_callback_thread_begin` +* `ompt_callback_thread_end` +* `ompt_callback_parallel_begin` +* `ompt_callback_parallel_end` +* `ompt_callback_implicit_task` +* `ompt_callback_work` +* `ompt_callback_master` +* `ompt_callback_sync_region` +* `ompt_callback_task_create` +* `ompt_callback_task_shcedule` +* `ompt_callback_sync_region_wait` +* `ompt_callback_mutex_released` +* `ompt_callback_dependences` +* `ompt_callback_task_dependence` +* `ompt_callback_lock_init` +* `ompt_callback_lock_destory` +* `ompt_callback_mutex_acquire` +* `ompt_callback_mutex_acquired` +* `ompt_callback_nest_lock` +* `ompt_callback_flush` +* `ompt_callback_cancel` + +The following callbacks are not implemented in LLVM +(9.0.0, tag: llvmorg-9.0.0), so are subject of the +future work: + +* `ompt_callback_target` +* `ompt_callback_target_data_op` +* `ompt_callback_target_submit` +* `ompt_callback_control_tool` +* `ompt_callback_device_initialize` +* `ompt_callback_device_finalize` +* `ompt_callback_device_load` +* `ompt_callback_device_unload` +* `ompt_callback_target_map` +* `ompt_callback_reduction` +* `ompt_callback_dispatch` + +## Additional information + +* Tracing is done on per worker basis, in oppose to per +core basis, so states are bound to the thread they happened +at. In the case of one-to-one mapping between cores and +threads per worker traces can be treated as per core traces +from the moment affinity of the thread was set. + +* It is assumed that states finish in the reverse order +to their starting order. For example if B starts after A +then B will finish before A. We also assume the state does +NOT move between threads. It should be case for any +standard constructs in OpenMP. This assumption allows +handling of nested constructs with a single unified +stack on every thread. + +## Dependencies + +The tools was build and tested with following dependencies: + +* ubuntu 18.04 LTS +* [aftermath](https://github.com/pepperpots/aftermath) (branch: afterompt-support) +* [llvm-project](https://github.com/llvm/llvm-project) (tag: llvmorg-9.0.0) +* clang (6.0.0-1ubuntu2) + +However it should work with any major compiler and runtime, +and operating system that support OMPT. Please note that +default system package may be built with OMPT disabled. + +## Build + +Before building the library the [llvm project](https://github.com/llvm/llvm-project) +and [aftermath](https://www.aftermath-tracing.com/prerelease/) have to be +built. Instructions for that can be found at corresponding websites. + +The next step is to export required variables: + +``` +export CMAKE_PREFIX_PATH="/install" + +export CC="clang" +export CXX="clang++" + +export C_INCLUDE_PATH="/install/lib/clang/9.0.0/include" +export CXX_INCLUDE_PATH="/install/lib/clang/9.0.0/include" +``` + +Then the library can be build and installed as follows: + +``` +mkdir build +cd build/ +cmake -DCMAKE_BUILD_TYPE=Release .. +make install +``` + +The library is installed in the project's root inside `install/` directory. + +## Usage + +Any OpenMP application can be run with tool attached to it generate +Aftermath traces. The following instruction outlines one of the +possible ways to do it: + +``` +export AFTEROMPT_LIBRARY_PATH="/install" +export LLVM_LIBRARY_PATH="/install/lib" + +source /env.sh + +clang -fopenmp -o omp-program omp-program.c + +AFTERMATH_TRACE_FILE=trace.ost \ +LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${LLVM_LIBRARY_PATH}:${AFTEROMPT_LIB_PATH} \ +LD_PRELOAD=${LLVM_LIBRARY_PATH}/libomp.so:${AFTEROMPT_LIBRARY_PATH}/libafterompt.so \ +./omp-program +``` + +In the given example the tool is dynamically attached to the runtime by using +the LD_PRELOAD variable. The LLVM runtime is also pre-loaded to ensure +that the system runtime is not used. + diff --git a/src/afterompt.c b/src/afterompt.c new file mode 100644 index 0000000..6aed207 --- /dev/null +++ b/src/afterompt.c @@ -0,0 +1,466 @@ +/** + * Copyright (C) 2018 Andi Drebes + * Copyright (C) 2019 Igor Wodiany + * + * Afterompt is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +// TODO: The root cause of that in Aftermath should be found +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Waddress-of-packed-member" + +#include +#include +#include + +#include + +#include + +#include "trace.h" + +#include "afterompt.h" + +#define DEBUG_INFO 0 + +/* Time reference */ +static struct am_timestamp_reference am_ompt_tsref; + +/* Pthread key to access thread tracing data */ +static pthread_key_t am_thread_data_key; + +ompt_set_callback_t am_set_callback; + +ompt_start_tool_result_t* ompt_start_tool(unsigned int omp_version, + const char* runtime_version) { +#if DEBUG_INFO + printf("%s (omp ver. %d)\n", runtime_version, omp_version); +#endif + + static ompt_start_tool_result_t ompt_start_tool_result = { + &ompt_initialize, &ompt_finalize, {}}; + + return &ompt_start_tool_result; +} + +#define REGISTER_CALLBACK(name) \ + if (am_set_callback(ompt_##name, (ompt_callback_t)&am_##name) < 2) { \ + fprintf(stderr, "Afterompt: Callback %s seems to be unsupported!\n", \ + #name); \ + } + +int ompt_initialize(ompt_function_lookup_t lookup, int num, ompt_data_t* data) { + am_set_callback = (ompt_set_callback_t)lookup("ompt_set_callback"); + + REGISTER_CALLBACK(callback_thread_begin); + REGISTER_CALLBACK(callback_thread_end); + REGISTER_CALLBACK(callback_parallel_begin); + REGISTER_CALLBACK(callback_parallel_end); + REGISTER_CALLBACK(callback_implicit_task); + REGISTER_CALLBACK(callback_task_create); + REGISTER_CALLBACK(callback_task_schedule); + REGISTER_CALLBACK(callback_sync_region_wait); + REGISTER_CALLBACK(callback_mutex_released); + REGISTER_CALLBACK(callback_dependences); + REGISTER_CALLBACK(callback_task_dependence); + REGISTER_CALLBACK(callback_work); + REGISTER_CALLBACK(callback_master); + REGISTER_CALLBACK(callback_sync_region); + REGISTER_CALLBACK(callback_lock_init); + REGISTER_CALLBACK(callback_lock_destroy); + REGISTER_CALLBACK(callback_mutex_acquire); + REGISTER_CALLBACK(callback_mutex_acquired); + REGISTER_CALLBACK(callback_nest_lock); + REGISTER_CALLBACK(callback_flush); + REGISTER_CALLBACK(callback_cancel); + + am_timestamp_reference_init(&am_ompt_tsref, am_timestamp_now()); + + am_ompt_init_trace(); + + if (pthread_key_create(&am_thread_data_key, NULL)) { + fprintf(stderr, "Afterompt: Failed to create thread data key.\n"); + /* Zero means failure */ + return 0; + } + + /* In this context non-zero means success */ + return 1; +} + +void ompt_finalize(ompt_data_t* data) { + pthread_key_delete(am_thread_data_key); + + am_ompt_exit_trace(); +} + +/* + Returns the current timestamp normalized to the reference. If the result + would be negative, the process is aborted. +*/ +static inline am_timestamp_t am_ompt_now(void) { + am_timestamp_t now; + + if (am_timestamp_reference_now(&am_ompt_tsref, &now)) { + fprintf( + stderr, + "Afterompt: Local timestamp normalized to reference is negative.\n"); + // TODO: Dying may be too radical + exit(1); + } + + return now; +} + +/* Push state on the state stack */ +static inline void am_ompt_push_state(struct am_ompt_thread_data* td, + am_timestamp_t tsc, uint64_t data) { + if (td->state_stack_top >= AM_OMPT_DEFAULT_MAX_STATE_STACK_ENTRIES) { + fprintf(stderr, "Afterompt: Could not push state. \n"); + // TODO: Dying may be too radical + exit(1); + } + + td->state_stack[td->state_stack_top].tsc = tsc; + td->state_stack[td->state_stack_top].data = data; + + td->state_stack_top++; +} + +/* Pop state from the state stack */ +static inline struct am_ompt_stack am_ompt_pop_state( + struct am_ompt_thread_data* td) { + if (td->state_stack_top == 0) { + fprintf(stderr, "Afterompt: Could not pop state.\n"); + // TODO: Dying may be too radical + exit(1); + } + + td->state_stack_top--; + + struct am_ompt_stack result = {td->state_stack[td->state_stack_top].tsc, + td->state_stack[td->state_stack_top].data}; + + return result; +} + +static inline struct am_ompt_thread_data* am_get_thread_data() { + struct am_ompt_thread_data* td; + + if (!(td = pthread_getspecific(am_thread_data_key))) { + fprintf(stderr, "Afterompt: Could not read thread data\n"); + // TODO: Dying may be too radical + exit(1); + } + + return td; +} + +void am_callback_thread_begin(ompt_thread_t type, ompt_data_t* data) { + struct am_ompt_thread_data* td; + + if (!(td = am_ompt_create_thread_data(pthread_self()))) { + fprintf(stderr, "Afterompt: Could not create thread data\n"); + // TODO: Dying may be too radical + exit(1); + } + + am_ompt_push_state(td, am_ompt_now(), type); + + pthread_setspecific(am_thread_data_key, td); +} + +void am_callback_thread_end(ompt_data_t* data) { + struct am_ompt_thread_data* td = am_get_thread_data(); + struct am_buffered_event_collection* c = td->event_collection; + + struct am_ompt_stack state = am_ompt_pop_state(td); + + struct am_dsk_interval interval = {state.tsc, am_ompt_now()}; + + struct am_dsk_openmp_thread t = {c->id, interval, state.data}; + + am_dsk_openmp_thread_write_to_buffer_defid(&c->data, &t); + + am_ompt_destroy_thread_data(td); +} + +void am_callback_parallel_begin(ompt_data_t* task_data, + const ompt_frame_t* task_frame, + ompt_data_t* parallel_data, + unsigned int requested_parallelism, int flags, + const void* codeptr_ra) { + am_ompt_push_state(am_get_thread_data(), am_ompt_now(), requested_parallelism); +} + +void am_callback_parallel_end(ompt_data_t* parallel_data, + ompt_data_t* task_data, int flags, + const void* codeptr_ra) { + struct am_ompt_thread_data* td = am_get_thread_data(); + struct am_buffered_event_collection* c = td->event_collection; + + struct am_ompt_stack state = am_ompt_pop_state(td); + + struct am_dsk_interval interval = {state.tsc, am_ompt_now()}; + + struct am_dsk_openmp_parallel p = {c->id, interval, state.data, flags}; + + am_dsk_openmp_parallel_write_to_buffer_defid(&c->data, &p); +} + +void am_callback_task_create(ompt_data_t* task_data, + const ompt_frame_t* task_frame, + ompt_data_t* new_task_data, int flags, + int has_dependences, const void* codeptr_ra) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_task_create tc = {c->id, am_ompt_now(), flags, + has_dependences}; + + am_dsk_openmp_task_create_write_to_buffer_defid(&c->data, &tc); +} + +void am_callback_task_schedule(ompt_data_t* prior_task_data, + ompt_task_status_t prior_task_status, + ompt_data_t* next_task_data) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_task_schedule ts = {c->id, am_ompt_now(), + prior_task_status}; + + am_dsk_openmp_task_schedule_write_to_buffer_defid(&c->data, &ts); +} + +void am_callback_implicit_task(ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, + ompt_data_t task_data, + unsigned int actual_parallelism, + unsigned int index, int flags) { + struct am_ompt_thread_data* td = am_get_thread_data(); + struct am_buffered_event_collection* c = td->event_collection; + + if (endpoint == ompt_scope_begin) { + am_ompt_push_state(td, am_ompt_now(), actual_parallelism); + } + else{ + struct am_ompt_stack state = am_ompt_pop_state(td); + + struct am_dsk_interval interval = {state.tsc, am_ompt_now()}; + + struct am_dsk_openmp_implicit_task it = {c->id, interval, state.data, flags}; + + am_dsk_openmp_implicit_task_write_to_buffer_defid(&c->data, &it); + } +} + +void am_callback_sync_region_wait(ompt_sync_region_t kind, + ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, + ompt_data_t* task_data, + const void* codeptr_ra) { + struct am_ompt_thread_data* td = am_get_thread_data(); + struct am_buffered_event_collection* c = td->event_collection; + + if (endpoint == ompt_scope_begin) { + am_ompt_push_state(td, am_ompt_now(), 0); + } + else{ + struct am_ompt_stack state = am_ompt_pop_state(td); + + struct am_dsk_interval interval = {state.tsc, am_ompt_now()}; + + struct am_dsk_openmp_sync_region_wait srw = {c->id, interval, kind}; + + am_dsk_openmp_sync_region_wait_write_to_buffer_defid(&c->data, &srw); + } +} + +void am_callback_mutex_released(ompt_mutex_t kind, ompt_wait_id_t wait_id, + const void* codeptr_ra) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_mutex_released mr = {c->id, am_ompt_now(), wait_id, + kind}; + + am_dsk_openmp_mutex_released_write_to_buffer_defid(&c->data, &mr); +} + +void am_callback_dependences(ompt_data_t* task_data, + const ompt_dependence_t* deps, int ndeps) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + /* We could collect more information here by traversing the deps + list to get the storage location of dependences. */ + struct am_dsk_openmp_dependences d = {c->id, am_ompt_now(), ndeps}; + + am_dsk_openmp_dependences_write_to_buffer_defid(&c->data, &d); +} + +void am_callback_task_dependence(ompt_data_t* src_task_data, + ompt_data_t* sink_task_data) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_task_dependence td = {c->id, am_ompt_now()}; + + am_dsk_openmp_task_dependence_write_to_buffer_defid(&c->data, &td); +} + +void am_callback_work(ompt_work_t wstype, ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, ompt_data_t* task_data, + uint64_t count, const void* codeptr_ca) { + struct am_ompt_thread_data* td = am_get_thread_data(); + struct am_buffered_event_collection* c = td->event_collection; + + if (endpoint == ompt_scope_begin) { + am_ompt_push_state(td, am_ompt_now(), count); + } + else{ + struct am_ompt_stack state = am_ompt_pop_state(td); + + struct am_dsk_interval interval = {state.tsc, am_ompt_now()}; + + struct am_dsk_openmp_work w = {c->id, interval, wstype, state.data}; + + am_dsk_openmp_work_write_to_buffer_defid(&c->data, &w); + } +} + +void am_callback_master(ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, ompt_data_t* taks_data, + const void* codeptr_ra) { + struct am_ompt_thread_data* td = am_get_thread_data(); + struct am_buffered_event_collection* c = td->event_collection; + + if (endpoint == ompt_scope_begin) { + am_ompt_push_state(td, am_ompt_now(), 0); + } + else{ + struct am_ompt_stack state = am_ompt_pop_state(td); + + struct am_dsk_interval interval = {state.tsc, am_ompt_now()}; + + struct am_dsk_openmp_master m = {c->id, interval}; + + am_dsk_openmp_master_write_to_buffer_defid(&c->data, &m); + } +} + +void am_callback_sync_region(ompt_sync_region_t kind, + ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, ompt_data_t* task_data, + const void* codeptr_ra) { + struct am_ompt_thread_data* td = am_get_thread_data(); + struct am_buffered_event_collection* c = td->event_collection; + + if (endpoint == ompt_scope_begin) { + am_ompt_push_state(td, am_ompt_now(), 0); + } + else{ + struct am_ompt_stack state = am_ompt_pop_state(td); + + struct am_dsk_interval interval = {state.tsc, am_ompt_now()}; + + struct am_dsk_openmp_sync_region sr = {c->id, interval, kind}; + + am_dsk_openmp_sync_region_write_to_buffer_defid(&c->data, &sr); + } +} + +void am_callback_lock_init(ompt_mutex_t kind, ompt_wait_id_t wait_id, + const void* codeptr_ra) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_lock_init li = {c->id, am_ompt_now(), wait_id, kind}; + + am_dsk_openmp_lock_init_write_to_buffer_defid(&c->data, &li); +} + +void am_callback_lock_destroy(ompt_mutex_t kind, ompt_wait_id_t wait_id, + const void* codeptr_ra) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_lock_destroy ld = {c->id, am_ompt_now(), wait_id, kind}; + + am_dsk_openmp_lock_destroy_write_to_buffer_defid(&c->data, &ld); +} + +void am_callback_mutex_acquire(ompt_mutex_t kind, unsigned int hint, + unsigned int impl, ompt_wait_id_t wait_id, + const void* codeptr_pa) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_mutex_acquire ma = {c->id, am_ompt_now(), wait_id, + kind, hint, impl}; + + am_dsk_openmp_mutex_acquire_write_to_buffer_defid(&c->data, &ma); +} + +void am_callback_mutex_acquired(ompt_mutex_t kind, ompt_wait_id_t wait_id, + const void* codeptr_ra) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_mutex_acquired ma = {c->id, am_ompt_now(), wait_id, + kind}; + + am_dsk_openmp_mutex_acquired_write_to_buffer_defid(&c->data, &ma); +} + +void am_callback_nest_lock(ompt_scope_endpoint_t endpoint, + ompt_wait_id_t wait_id, const void* codeptr_ra) { + struct am_ompt_thread_data* td = am_get_thread_data(); + struct am_buffered_event_collection* c = td->event_collection; + + if (endpoint == ompt_scope_begin) { + am_ompt_push_state(td, am_ompt_now(), 0); + } + else{ + struct am_ompt_stack state = am_ompt_pop_state(td); + + struct am_dsk_interval interval = {state.tsc, am_ompt_now()}; + + struct am_dsk_openmp_nest_lock nl = {c->id, interval, wait_id}; + + am_dsk_openmp_nest_lock_write_to_buffer_defid(&c->data, &nl); + } +} + +void am_callback_flush(ompt_data_t* thread_data, const void* codeptr_ra) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_flush f = {c->id, am_ompt_now()}; + + am_dsk_openmp_flush_write_to_buffer_defid(&c->data, &f); +} + +void am_callback_cancel(ompt_data_t* task_data, int flags, + const void* codeptr_ra) { + struct am_buffered_event_collection* c = + am_get_thread_data()->event_collection; + + struct am_dsk_openmp_cancel cc = {c->id, am_ompt_now(), flags}; + + am_dsk_openmp_cancel_write_to_buffer_defid(&c->data, &cc); +} + +#pragma clang pop diff --git a/src/afterompt.h b/src/afterompt.h new file mode 100644 index 0000000..2e11137 --- /dev/null +++ b/src/afterompt.h @@ -0,0 +1,112 @@ +/** + * Copyright (C) 2018 Andi Drebes + * Copyright (C) 2019 Igor Wodiany + * + * Afterompt is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include + +/* All function signatures as defined in OpenMP API Specification 5.0 */ + +/* Tool setup */ +ompt_start_tool_result_t* ompt_start_tool(unsigned int omp_version, + const char* runtime_version); + +/* Tool initialization */ +int ompt_initialize(ompt_function_lookup_t lookup, int num, ompt_data_t* data); + +/* Tool clean up */ +void ompt_finalize(ompt_data_t* data); + +/* Callbacks */ +void am_callback_thread_begin(ompt_thread_t type, ompt_data_t* data); + +void am_callback_thread_end(ompt_data_t* data); + +void am_callback_parallel_begin(ompt_data_t* task_data, + const ompt_frame_t* task_frame, + ompt_data_t* parallel_data, + unsigned int requested_parallelism, int flags, + const void* codeptr_ra); + +void am_callback_parallel_end(ompt_data_t* parallel_data, + ompt_data_t* task_data, int flags, + const void* codeptr_ra); + +void am_callback_task_create(ompt_data_t* task_data, + const ompt_frame_t* task_frame, + ompt_data_t* new_task_data, int flags, + int has_dependences, const void* codeptr_ra); + +void am_callback_task_schedule(ompt_data_t* prior_task_data, + ompt_task_status_t prior_task_status, + ompt_data_t* next_task_data); + +void am_callback_implicit_task(ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, + ompt_data_t task_data, + unsigned int actual_parallelism, + unsigned int index, int flags); + +void am_callback_sync_region_wait(ompt_sync_region_t kind, + ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, + ompt_data_t* task_data, + const void* codeptr_ra); + +void am_callback_mutex_released(ompt_mutex_t kind, ompt_wait_id_t wait_id, + const void* codeptr_ra); + +void am_callback_dependences(ompt_data_t* task_data, + const ompt_dependence_t* deps, int ndeps); + +void am_callback_task_dependence(ompt_data_t* src_task_data, + ompt_data_t* sink_task_data); + +void am_callback_work(ompt_work_t wstype, ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, ompt_data_t* task_data, + uint64_t count, const void* codeptr_ca); + +void am_callback_master(ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, ompt_data_t* taks_data, + const void* codeptr_ra); + +void am_callback_sync_region(ompt_sync_region_t kind, + ompt_scope_endpoint_t endpoint, + ompt_data_t* parallel_data, ompt_data_t* task_data, + const void* codeptr_ra); + +void am_callback_lock_init(ompt_mutex_t kind, ompt_wait_id_t wait_id, + const void* codeptr_ra); + +void am_callback_lock_destroy(ompt_mutex_t kind, ompt_wait_id_t wait_id, + const void* codeptr_ra); + +void am_callback_mutex_acquire(ompt_mutex_t kind, unsigned int hint, + unsigned int impl, ompt_wait_id_t wait_id, + const void* codeptr_pa); + +void am_callback_mutex_acquired(ompt_mutex_t kind, ompt_wait_id_t wait_id, + const void* codeptr_ra); + +void am_callback_nest_lock(ompt_scope_endpoint_t endpoint, + ompt_wait_id_t wait_id, const void* codeptr_ra); + +void am_callback_flush(ompt_data_t* thread_data, const void* codeptr_ra); + +void am_callback_cancel(ompt_data_t* task_data, int flags, + const void* codeptr_ra); + diff --git a/src/trace.c b/src/trace.c new file mode 100644 index 0000000..447e6d6 --- /dev/null +++ b/src/trace.c @@ -0,0 +1,335 @@ +/** + * Copyright (C) 2018 Andi Drebes + * Copyright (C) 2019 Igor Wodiany + * + * Afterompt is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +// TODO: The root cause of that in Aftermath should be found +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Waddress-of-packed-member" + +#include +#include +#include + +#include +#include + +#include "trace.h" + +/* Application trace */ +struct am_buffered_trace am_ompt_trace; + +/* Name of the trace file */ +static const char* am_ompt_trace_file; + +/* Protected by trace_lock */ +static am_hierarchy_node_id_t curr_hierarchy_node_id = 2; + +/* Size of event collection write buffers */ +static size_t am_ompt_cbuf_size; + +/* Lock for trace-wide operations */ +static pthread_spinlock_t am_ompt_trace_lock; + +/* Create a new event collection and attach it to the trace */ +static struct am_buffered_event_collection* am_ompt_create_event_collection( + pthread_t tid) { + struct am_buffered_event_collection* c; + am_event_collection_id_t id; + struct am_dsk_event_collection dsk_ec; + struct am_dsk_hierarchy_node dsk_hn; + struct am_simple_hierarchy_node* hn; + char name_buf[64]; + + id = tid; + + /* Prepare on-disk structures before taking the lock */ + snprintf(name_buf, sizeof(name_buf), "%u", id); + dsk_ec.id = id; + dsk_ec.name.str = name_buf; + dsk_ec.name.len = strlen(name_buf); + + dsk_hn.parent_id = 1; + dsk_hn.name.str = name_buf; + dsk_hn.name.len = strlen(name_buf); + + if (!(c = malloc(sizeof(*c)))) { + fprintf(stderr, + "Afterompt: Could not allocate event " + "collection.\n"); + goto out_err; + } + + if (!(hn = malloc(sizeof(*hn)))) goto out_err_free_c; + + if (!(hn->name = strdup(name_buf))) goto out_err_free_hn; + + hn->first_child = NULL; + + if (am_buffered_event_collection_init(c, id, am_ompt_cbuf_size)) { + fprintf(stderr, + "Afterompt: Could not initialize event " + "collection.\n"); + goto out_err_free_hn_name; + } + + /* Use pthread lock rather than critical section to avoid + triggering callbacks before thread data is initialized */ + if (pthread_spin_lock(&am_ompt_trace_lock)) { + fprintf(stderr, "Afterompt: Could not acquire lock. \n"); + goto out_err_destroy; + } + + if (am_buffered_trace_add_collection(&am_ompt_trace, c)) { + fprintf(stderr, + "Afterompt: Could not add event collection " + "to trace.\n"); + goto out_err_unlock; + } + + if (am_dsk_event_collection_write_to_buffer_defid(&am_ompt_trace.data, + &dsk_ec)) { + fprintf(stderr, + "Afterompt: Could not trace event collection " + "frame.\n"); + goto out_err_unlock; + } + + hn->id = curr_hierarchy_node_id++; + dsk_hn.hierarchy_id = am_ompt_trace.hierarchies[0]->id; + dsk_hn.id = hn->id; + + am_simple_hierarchy_node_add_child(am_ompt_trace.hierarchies[0]->root, hn); + + if (am_dsk_hierarchy_node_write_to_buffer_defid(&am_ompt_trace.data, + &dsk_hn)) { + am_simple_hierarchy_node_remove_first_child( + am_ompt_trace.hierarchies[0]->root); + + fprintf(stderr, + "Afterompt: Could not trace hierrachy node " + "frame.\n"); + + goto out_err_unlock; + } + + if (pthread_spin_unlock(&am_ompt_trace_lock)) { + fprintf(stderr, "Afterompt: Could not release the lock. \n"); + goto out_err_destroy; + } + + return c; + +out_err_unlock: + if (pthread_spin_unlock(&am_ompt_trace_lock)) { + fprintf(stderr, "Afterompt: Could not release the lock. \n"); + } +out_err_destroy: + am_buffered_event_collection_destroy(c); +out_err_free_hn_name: + free(hn->name); +out_err_free_hn: + free(hn); +out_err_free_c: + free(c); +out_err: + return NULL; +} + +struct am_ompt_thread_data* am_ompt_create_thread_data(pthread_t tid) { + struct am_ompt_thread_data* data; + + if ((data = malloc(sizeof(*data))) == NULL) { + fprintf(stderr, "Afterompt: Could not allocate memory for thread data\n"); + goto out_err; + } + + if ((data->state_stack = malloc(sizeof(struct am_ompt_stack) * + AM_OMPT_DEFAULT_MAX_STATE_STACK_ENTRIES)) == + NULL) { + fprintf(stderr, "Afterompt: Could not allocate memory for state stack\n"); + goto out_err_free; + } + + data->state_stack_top = 0; + + if ((data->event_collection = am_ompt_create_event_collection(tid)) == NULL) { + fprintf(stderr, + "Afterompt: Could not create event collection for thread\n"); + goto out_err_destroy; + } + + return data; + +out_err_destroy: + free(data->state_stack); +out_err_free: + free(data); +out_err: + return NULL; +} + +void am_ompt_destroy_thread_data(struct am_ompt_thread_data* thread_data) { + free(thread_data->state_stack); + free(thread_data); + + // TODO: Event collection is not free, so it can be dumped on exit. +} + +/* Write default ID of each state to the trace */ +static int am_ompt_register_types() { + if (am_dsk_hierarchy_description_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_hierarchy_node_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_event_collection_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_event_mapping_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_openmp_thread_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_openmp_parallel_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_openmp_implicit_task_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_task_create_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_task_schedule_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_sync_region_wait_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_mutex_released_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_dependences_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_task_dependence_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_work_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_openmp_master_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_openmp_sync_region_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_lock_init_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_openmp_lock_destroy_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_mutex_acquire_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_mutex_acquired_write_default_id_to_buffer( + &am_ompt_trace.data) || + am_dsk_openmp_nest_lock_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_openmp_flush_write_default_id_to_buffer(&am_ompt_trace.data) || + am_dsk_openmp_cancel_write_default_id_to_buffer(&am_ompt_trace.data)) { + return 1; + } + + return 0; +} + +int am_ompt_init_trace() { + /* Size of trace-wide buffer */ + size_t tbuf_size; + const char* size; + + /* Get size for trace-wide buffer */ + if ((size = getenv("AFTERMATH_TRACE_BUFFER_SIZE"))) + sscanf(size, "%zu", &tbuf_size); + else + tbuf_size = AM_OMPT_DEFAULT_TRACE_BUFFER_SIZE; + + /* Get size for per event-collection buffers */ + if ((size = getenv("AFTERMATH_EVENT_COLLECTION_BUFFER_SIZE"))) { + sscanf(size, "%zu", &am_ompt_cbuf_size); + } else { + am_ompt_cbuf_size = AM_OMPT_DEFAULT_EVENT_COLLECTION_BUFFER_SIZE; + } + + /* Filename of the trace file */ + if (!(am_ompt_trace_file = getenv("AFTERMATH_TRACE_FILE"))) { + fprintf(stderr, "Afterompt: No trace file specified.\n"); + goto out_err; + } + + if (am_buffered_trace_init(&am_ompt_trace, tbuf_size)) { + fprintf(stderr, "Afterompt: Could not initialize trace.\n"); + goto out_err; + } + + if (!am_buffered_trace_new_hierarchy(&am_ompt_trace, "Workers", "\"\" {}")) { + fprintf(stderr, + "Afterompt: Could not create main " + "hierarchy.\n"); + goto out_err_trace; + } + + if (am_ompt_register_types()) goto out_err_trace; + + if (am_simple_hierarchy_write_to_buffer_defid(&am_ompt_trace.data, + am_ompt_trace.hierarchies[0])) { + fprintf(stderr, + "Afterompt: Could not write hierarchy " + "description and root node.\n"); + goto out_err_trace; + } + + if (pthread_spin_init(&am_ompt_trace_lock, 0)) { + fprintf(stderr, "Afterompt: Could not create spin lock.\n"); + goto out_err_trace; + } + + return 0; + +out_err_trace: + am_buffered_trace_destroy(&am_ompt_trace); +out_err: + return 1; +} + +/* Writes an entry for each event collection to the trace buffer */ +static int am_ompt_trace_mappings() { + struct am_dsk_event_mapping dsk_em; + + for (size_t i = 0; i < am_ompt_trace.num_collections; i++) { + dsk_em.collection_id = am_ompt_trace.collections[i]->id; + dsk_em.hierarchy_id = 0; + dsk_em.node_id = i + 2; + dsk_em.interval.start = 0; + dsk_em.interval.end = AM_TIMESTAMP_T_MAX; + + if (am_dsk_event_mapping_write_to_buffer_defid(&am_ompt_trace.data, + &dsk_em)) { + fprintf(stderr, + "Afterompt: Could not write event " + "mapping for event collection %u" + " .\n", + am_ompt_trace.collections[i]->id); + return 1; + } + } + + return 0; +} + +void am_ompt_exit_trace() { + if (am_ompt_trace_mappings()) { + fprintf(stderr, "Afterompt: Could not trace event mappings.\n"); + } + + if (am_buffered_trace_dump(&am_ompt_trace, am_ompt_trace_file)) { + fprintf(stderr, + "Afterompt: Could not write trace file " + "\"%s\".\n", + am_ompt_trace_file); + } + + am_buffered_trace_destroy(&am_ompt_trace); + pthread_spin_destroy(&am_ompt_trace_lock); +} + +#pragma clang diagnostic pop diff --git a/src/trace.h b/src/trace.h new file mode 100644 index 0000000..3f350b4 --- /dev/null +++ b/src/trace.h @@ -0,0 +1,68 @@ +/** + * Copyright (C) 2018 Andi Drebes + * Copyright (C) 2019 Igor Wodiany + * + * Afterompt is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef AM_OMPT_TRACE_H +#define AM_OMPT_TRACE_H + +#include +#include +#include + +#define AM_OMPT_DEFAULT_TRACE_BUFFER_SIZE (2 << 20) +#define AM_OMPT_DEFAULT_EVENT_COLLECTION_BUFFER_SIZE (2 << 24) +#define AM_OMPT_DEFAULT_MAX_STATE_STACK_ENTRIES 64 + +/* Application trace */ +extern struct am_buffered_trace am_ompt_trace; + +/* Stack for tracing states containing intervals */ +struct am_ompt_stack { + am_timestamp_t tsc; + uint64_t data; +}; + +/* Struct containing tracing information for a single thread */ +struct am_ompt_thread_data { + struct am_buffered_event_collection* event_collection; + struct am_ompt_stack* state_stack; + uint32_t state_stack_top; +}; + +/* + Initialize new trace. The function has to be called before any + other tracing related function is called. +*/ +int am_ompt_init_trace(); + +/* + Initialize an event collection and state stack for a specific + thread and add add them to the trace. +*/ +struct am_ompt_thread_data* am_ompt_create_thread_data(pthread_t tid); + +/* + Free the core data and destroy the state stack. +*/ +void am_ompt_destroy_thread_data(struct am_ompt_thread_data* thread_data); + +/* + Save trace to the file and clean up all structures. +*/ +void am_ompt_exit_trace(); + +#endif