{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import trappy\n",
    "from trappy import ftrace\n",
    "from trappy.ftrace import GenericFTrace\n",
    "import numpy as np\n",
    "import pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lines = [\n",
    "\"            adbd-5709  [007]  2943.184105: sched_contrib_scale_f: cpu=7 cpu_scale_factor=1\\n\"\n",
    "\"            adbd-5709  [007]  2943.184105: sched_load_avg_cpu:   cpu=7 util_avg=825\\n\"\n",
    "\"     ->transport-5713  [006]  2943.184106: sched_load_avg_cpu:   cpu=6 util_avg=292\\n\"\n",
    "\"     ->transport-5713  [006]  2943.184107: sched_contrib_scale_f: cpu=6 cpu_scale_factor=2\\n\"\n",
    "\"            adbd-5709  [007]  2943.184108: sched_load_avg_cpu:   cpu=7 util_avg=850\\n\"\n",
    "\"            adbd-5709  [007]  2943.184109: sched_contrib_scale_f: cpu=7 cpu_scale_factor=3\\n\"\n",
    "\"            adbd-5709  [007]  2943.184110: sched_load_avg_cpu:   cpu=6 util_avg=315\\n\"\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('/tmp/trappy_mergedf_example.txt', 'w') as fh:\n",
    "    for line in lines:\n",
    "        fh.write(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            adbd-5709  [007]  2943.184105: sched_contrib_scale_f: cpu=7 cpu_scale_factor=1\r\n",
      "            adbd-5709  [007]  2943.184105: sched_load_avg_cpu:   cpu=7 util_avg=825\r\n",
      "     ->transport-5713  [006]  2943.184106: sched_load_avg_cpu:   cpu=6 util_avg=292\r\n",
      "     ->transport-5713  [006]  2943.184107: sched_contrib_scale_f: cpu=6 cpu_scale_factor=2\r\n",
      "            adbd-5709  [007]  2943.184108: sched_load_avg_cpu:   cpu=7 util_avg=850\r\n",
      "            adbd-5709  [007]  2943.184109: sched_contrib_scale_f: cpu=7 cpu_scale_factor=3\r\n",
      "            adbd-5709  [007]  2943.184110: sched_load_avg_cpu:   cpu=6 util_avg=315\r\n"
     ]
    }
   ],
   "source": [
    "!cat /tmp/trappy_mergedf_example.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "trace = ftrace.FTrace('/tmp/trappy_mergedf_example.txt', events=['sched_contrib_scale_f', 'sched_load_avg_cpu'], normalize_time=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "      <th>util_avg</th>\n",
       "      <th>__line</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2943.184105</th>\n",
       "      <td>7</td>\n",
       "      <td>825</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2943.184106</th>\n",
       "      <td>6</td>\n",
       "      <td>292</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2943.184108</th>\n",
       "      <td>7</td>\n",
       "      <td>850</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2943.184110</th>\n",
       "      <td>6</td>\n",
       "      <td>315</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             cpu  util_avg  __line\n",
       "Time                              \n",
       "2943.184105    7       825       1\n",
       "2943.184106    6       292       2\n",
       "2943.184108    7       850       4\n",
       "2943.184110    6       315       6"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = trace.sched_load_avg_cpu.data_frame[['cpu', 'util_avg', '__line']]\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "      <th>cpu_scale_factor</th>\n",
       "      <th>__line</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2943.184105</th>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2943.184107</th>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2943.184109</th>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             cpu  cpu_scale_factor  __line\n",
       "Time                                      \n",
       "2943.184105    7                 1       0\n",
       "2943.184107    6                 2       3\n",
       "2943.184109    7                 3       5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = trace.sched_contrib_scale_f.data_frame[['cpu', 'cpu_scale_factor', '__line']]\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/joelaf/repo/lisa-aosp/external/trappy/trappy/utils.py:154: FutureWarning: sort(columns=....) is deprecated, use sort_values(by=.....)\n",
      "  df = pd.concat([pr_df, sec_df], keys=['primary', 'secondary']).sort(columns='__line')\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>__line</th>\n",
       "      <th>cpu</th>\n",
       "      <th>cpu_scale_factor</th>\n",
       "      <th>util_avg</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2943.184105</th>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>1.0</td>\n",
       "      <td>825.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2943.184106</th>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>292.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2943.184108</th>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>1.0</td>\n",
       "      <td>850.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2943.184110</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>315.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             __line  cpu  cpu_scale_factor  util_avg\n",
       "Time                                                \n",
       "2943.184105       1    7               1.0     825.0\n",
       "2943.184106       2    6               NaN     292.0\n",
       "2943.184108       4    7               1.0     850.0\n",
       "2943.184110       6    6               2.0     315.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trappy.utils.merge_dfs(df1, df2, 'cpu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "gist": {
   "data": {
    "description": "TRAPpy_MergeDF_API_Example",
    "public": false
   },
   "id": ""
  },
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "12px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}