If $\frac{\partial}{\partial s}g=v$, then $\frac{\partial R}{\partial
s}=-\Delta V+\operatorname{div}^{2}v-\left\langle v,\operatorname{Ric}
\right\rangle $ and $\frac{\partial}{\partial s}d\mu=\frac{1}{2}Vd\mu$, where
$V=\operatorname{tr}_{g}v$. So
$$
\frac{\partial}{\partial s}(Rd\mu)=(-\Delta V+\operatorname{div}^{2}v+\langle
v,\tfrac{R}{2}g-\operatorname{Ric}\rangle)d\mu.
$$
Integrating this, we see that the Euler-Lagrange equation for $\int Rd\mu$ is
$\tfrac{R}{2}g-\operatorname{Ric}=0$ (Einstein-Hilbert). To get Ricci flow, we
want to get rid of the $\tfrac{R}{2}g$ term due to the variation of the volume
form. Perelman accomplished this by introducing $f$ with $e^{-f}d\mu$ fixed.
Imposing $\frac{\partial}{\partial s}\left( e^{-f}d\mu\right) =0$, i.e.,
$\frac{\partial f}{\partial s}=\frac{V}{2}$, we obtain
$$
\frac{d}{ds}\int Re^{-f}d\mu=-\int\left\langle v,\operatorname{Ric}
\right\rangle e^{-f}d\mu+\int\left( -\Delta V+\operatorname{div}^{2}v\right)
e^{-f}d\mu.
$$
One pays the price that the divergence terms no longer integrate to zero.
Serendipitously,
$$
\frac{d}{ds}\int\left\vert \nabla f\right\vert ^{2}e^{-f}d\mu=\int\left(
-v\left( \nabla f,\nabla f\right) +\Delta V\right) e^{-f}d\mu,
$$
so, by combining the above and integrating by parts, one obtains Perelman's
formula:
$$
\frac{d}{ds}\int\left( R+\left\vert \nabla f\right\vert ^{2}\right)
e^{-f}d\mu=-\int\left\langle v,\operatorname{Ric}+\nabla^{2}f\right\rangle
e^{-f}d\mu.
$$
The gradient flow is $\frac{\partial}{\partial t}g=-2(\operatorname{Ric}
+\nabla^{2}f)$, $\frac{\partial f}{\partial t}=-R-\Delta f$. One cannot solve
this forward, so one makes the gauge change: $\frac{\partial}{\partial
t}g=-2\operatorname{Ric},$ $\frac{\partial f}{\partial t}=-R-\Delta
f+\left\vert \nabla f\right\vert ^{2}$ (by adding $\mathcal{L}_{\nabla f}$ to
both equations). Since we have decoupled the first equation from the second,
we can solve it forward in time (Hamilton-DeTurck). For applications, $f$ is
solved backward in time. For geometric flows, the idea of using the backward
heat kernel to obtain a monotonicity formula was originally used by Gerhard Huisken
for the mean curvature flow and by Michael Struwe for the harmonic map heat flow.
Hamilton tried to get this to work for Ricci flow; his interest is evident
from his paper with Matt Grayson.