An approach and system for engine and aftertreatment system optimization. Emissions of an engine may be reduced by an aftertreatment mechanism. Control of the engine and the aftertreatment mechanism may be coordinated for the best overall efficiency relative to both fuel consumption and the emissions reduction. Engine and aftertreatment control may also be optimized in terms of cost function minimization. Individual efficiencies of the engine and aftertreatment mechanism are not necessarily significant by themselves. Therefore, the engine and aftertreatment mechanism should be controlled in a manner to achieve the optimality of the engine and the aftertreatment mechanism together as one entity.