Potential fix for race condition.
Limit each StreamHandler to only see log requests for its thread.
Update stack trace while moving sampling slider

Summary: Not just when releasing! I definitely thought this worked before, but perhaps not...

Test Plan: Dragged the "Time" slider for the sampling profiler, and saw the stack trace update while I dragged it, not just after mouseup.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D7255
Fix occasional crash in the sampling profiler

Summary:
Before this commit, when the sampling profiler was finished, the main thread
would set a flag for the sampling thread to stop profiling but wouldn't wait for
the background thread to actually exit. This meant that the background thread
would sometimes add one last sample to the list of samples while we were in the
middle of processing that list. The Profile.results function makes two passes
over the list of samples and assumes that they'll be the same both times, so it
was crashing when the list got modified in the middle.

The fix is to wait for the sampling thread to exit before processing the
results.

Test Plan:
Temporarily put a sleep(1) just before the crash point and a sleep(.01) at the
top of the top of the sampling loop. With these sleeps, the home page
consistently crashes with sampling profiling enabled. Adding the join statement
fixes this crash.

Reviewers: ben

Reviewed By: ben

Subscribers: alpert

Differential Revision: http://phabricator.khanacademy.org/D7621
Add option to sampling profiler to additionally sample memory.

Summary:
This adds an option to additionally sample memory.  Since memory sampling is
slow on prod, it does it fairly infrequently.  (After some further profiling,
we may be able to turn it up a bit.)

Currently it does nothing on a dev server, since the dev server does not allow
importing the python module, and does not return useful data from the prod
runtime API.

Screenshots:
{F23421}
{F23422}

Test Plan:
Set the dev server fallback to return random data instead of zero, to check
that the interface work.  Test the profiler in the dev shell.  Will also test
on staging.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D10320
Fixes two little bugs in sampling memory profiler.

The display was breaking if memory sampling was off because I was using jQuery
incorrectly, and I forgot to add the new option in one place.

Test Plan:
Load a page with the miniprofiler, with memory sampling both on and off.  Check
that the profile renders correctly in both cases.

Auditors: alan
Adds missing import to memory sampling profiler.

Test Plan: Fingers crossed.

Auditors: alan
Ignore GetSystemStatsRequest in duplicate RPC detection.

Summary:
The memory profiler generates a lot of repeated calls to runtime.memory_usage()
on prod.  These get marked as duplicate RPCs, even though they are intentional.
More generally, two calls to the runtime API may return different data, and
were likely intentionally duplicated, so they should not get marked as such in
the profiler.

Test Plan:
Load /stats/memory (which makes multiple runtime.memory_usage() calls), check
that they are no longer marked as duplicate RPCs.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D10411
Improves sampling memory profiler display.

Summary:
It will now show both the next and previous memory samples and the change
between them, and show them in a nicer format.

Screenshot: {F25090}

Test Plan: Load the homepage with and without memory sampling enabled.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D10576
Increase memory sample rate.

Summary:
The google.appengine.api.runtime API is faster than I thought, so it should be
fine to bump up our sample rate.  This will increase the overhead a bit but
also give more useful data.

Test Plan: Load the homepage with the memory sampling profiler enabled.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D10577
Add a memory overview line when memory sampling is on.

Summary:
Shows the initial, final, and maximum memory.  This should make it easier to
see when there may be something worth looking at in the memory profile.

Test Plan: Load the homepage.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D10625
Fix to sampling profiler when memory is turned off.

Test Plan: Load the homepage with sampling profiler, with and without memory sampling.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D10850
Add (M) icon for high-memory requests.

Summary:
It goes right next to the (D) for duplicate RPCs.  It is pink for very
high-memory requests (>10MB).

Test Plan:
Load pages, check that the icon appears when it should, and is the right color
on various backgrounds.  Check that it doesn't break other profiling modes.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D11243
Make mini profiler detail window float at the top of the page

Summary:
Previously if you had a really long list of AJAX requests in the mini profiler,
you would have to scroll down, click on one, and then scroll way up to the
detail window.  This way, the detail window will float down the page with you.
{F33796}

Test Plan: Load a page, get it to make lots of AJAX requests.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D11272
Add sampling profiler method to dump to .cpuprofile format.

Summary:
It's currently not hooked up to anything, but if you run the sampling profiler
manually, write Profiler.cpuprofiler_results() to a file, and upload that to
Chrome, it should work.
{F40100}

Known issue: the very last sample gets displayed a bit weirdly.  I'll fix this if I
have time, but it's not a huge issue since in real things we want to optimize
we'll probably have tens or hundreds of samples.

Test Plan:
Profiled time.sleep() from the dev shell, loaded the profile into chrome,
checked that things looked reasonable.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D11844
Add link to .cpuprofile to GAE Mini Profiler.

Summary:
{F40129}
{F40131}

Test Plan: Load a page, check that it works and imports into chrome happily.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D11851
Allow passing in a function to use instead of time.time.

Summary: This is useful, for example, if time.time is mocked out in tests.

Test Plan:
Hooked it up to the test runner, it worked.  Tested in-browser that it didn't
break anything.

Reviewers: alan

Reviewed By: alan

Differential Revision: http://phabricator.khanacademy.org/D11904
Merge pull request #52 from rsamuelklatchko/master

Thread safety changes
Merge remote-tracking branch 'upstream/master'

Conflicts:
	profiler.py