diff --git a/doc/hgrc.5.txt b/doc/hgrc.5.txt
index 3f9ce63da18c22180f60c964f5b03ede7919f586_ZG9jL2hncmMuNS50eHQ=..637d4c0898348f3f428be752ed50230d85f73123_ZG9jL2hncmMuNS50eHQ= 100644
--- a/doc/hgrc.5.txt
+++ b/doc/hgrc.5.txt
@@ -412,7 +412,10 @@
     "hotshot" is deprecated, and produces less reliable results.
     Default is no profiling.
   output;;
-    The name of a file to write profiling data to.  Default is stderr.
+    The name of a file to write profiling data to.  Each occurrence of
+    "%%p" will be replaced with the current process ID (the repeated
+    "%" protects against the config parser's string interpolator).
+    Default output is to stderr.
   
 server::
   Controls generic server settings.
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
index 3f9ce63da18c22180f60c964f5b03ede7919f586_bWVyY3VyaWFsL2Rpc3BhdGNoLnB5..637d4c0898348f3f428be752ed50230d85f73123_bWVyY3VyaWFsL2Rpc3BhdGNoLnB5 100644
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -379,7 +379,8 @@
     def profile_fp():
         outfile = ui.config('profile', 'output', untrusted=True)
         if outfile:
-            return open(outfile, 'w')
+            pid = str(os.getpid())
+            return open(outfile.replace('%p', pid), 'w')
         else:
             return sys.stderr