{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode  # noqa: F401\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Profiling and Tracing\n",
    "\n",
    "* [Instance-based Profiling](inst_profile.ipynb)\n",
    "* [Memory Profiling](inst_mem_profile.ipynb)\n",
    "* [Instance-based Call Tracing](inst_call_tracing.ipynb)\n",
    "\n",
    "The profiling and call tracing tools mentioned above have a similar programmatic interface, even though most of the time they will only be used in command-line mode. However, if you really want to customize the set of methods that are to be profiled or traced, see the following example.\n",
    "\n",
    "``` python\n",
    "    # for performance profiling\n",
    "    from openmdao.devtools import iprofile as tool\n",
    "\n",
    "    # OR for call tracing\n",
    "    # from openmdao.devtools import itrace as tool\n",
    "\n",
    "    # First, make sure that the classes I use to define my method set exist in this\n",
    "    # namespace.\n",
    "    from mystuff import MyClass, MyOtherClass\n",
    "\n",
    "    # Let's say I only want to track methods with 'foo' or 'bar' in the name that belong to\n",
    "    # MyClass and ones with 'baz' in the name that belong to either MyClass or MyOtherClass.\n",
    "    # I use the following glob patterns and tuples of classes to specify this.\n",
    "    methods = [\n",
    "        ('*foo*', (MyClass,)),\n",
    "        ('*bar*', (MyClass,)),\n",
    "        ('*baz*', (MyClass, MyOtherClass))\n",
    "    ]\n",
    "\n",
    "    # set up the tool using my custom method set\n",
    "    tool.setup(methods=methods)\n",
    "\n",
    "    tool.start()\n",
    "\n",
    "    # run the code I want to profile/trace...\n",
    "\n",
    "    tool.stop()\n",
    "\n",
    "    # do some other stuff that I don't want to profile/trace...\n",
    "```\n",
    "\n",
    "* [Timing Systems under MPI](timing.ipynb)\n",
    "\n",
    "## Dumping debug info to file based on MPI rank and/or PID\n",
    "\n",
    "The `OPENMDAO_DUMP` environment variable can be set to a comma separated list of keywords to control \n",
    "what information is dumped and which file it is written to. Some valid example vaues are \n",
    "`stdout`, `stderr`, `rank`, `pid`, `rank,pid`, `pid,rank`, `rank,trace`, or `pid,trace,args`.\n",
    "\n",
    "- `rank` means to include the rank in the dump file name, e.g., om_dump_0.out\n",
    "- `pid` means to include the pid in the dump file name, e.g., om_dump_12345.out\n",
    "- `stdout` means to dump to stdout (so rank and pid are ignored)\n",
    "- `stderr` means to dump to stderr (so rank and pid are ignored)\n",
    "- `trace` means to show function entry and exit.\n",
    "- `args` means to include args and kwargs passed to functions if `trace` is active.\n",
    "\n",
    "if `rank` and `pid` are both included, the file name will include both, `rank` first, e.g., om_dump_0_12345.out\n",
    "\n",
    "If `OPENMDAO_DUMP` is just a plain truthy value, like '1', then output is dumped to a file named om_dump.out.\n",
    "\n",
    "The output written to the `om_dump` file will be the output of any `om_dump` calls that have been \n",
    "added to the code for debugging purposes, plus function entries and exits if `trace` is active.\n",
    "\n",
    "The specific behavior of the `trace` option can be specified by calling the `set_trace_predicate`\n",
    "method, which can be found in `openmdao.utils.general_utils`.  It takes a single argument which is\n",
    "a function of the form `myfunc(name, obj)`, where `name` is the name of the object `obj` in its parent\n",
    "container.  Note that `obj` is guaranteed to be a function."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
