How to count cycles used by a routine?
Just use the I/O pin toggle as you surmised. Toggling an I/O pin in 8051 code takes a known number of cycles and that can be subtracted from what ever scope measurement that you make. If the duration of the routine that you are measuring is many many instructions the additional time consumed toggling the I/O pin may be peanuts in comparison and could be basically ignored. As a matter of fact the measurement on the scope out at several milliseconds would barely register if a few hundred extra nanoseconds were included in the measurement. 

In my discussion above I am talking about using an I/O pin that is connected to one of the direct ports of the 8051 part. If your measuring I/O pin is remote from the MCU, such as an I/O on an I2C attached port expander, then the interface speed could very well be a factor to consider. Always use a direct port I/O for a scope timing measurement. 

If you want to actually see the time overhead of the I/O pin toggling simply put the set and clear of the I/O pin as sequential writes in your code and measure the resulting scope pulse.