Hashim Amla was one run shy of his 18th Test century as South Africa took control of the final Test against Australia in Perth.