Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.runtime.profile;
 
 
 import org.jruby.Ruby;
 
 public abstract class ProfilePrinter {
    
    
Printer implementation factory for supported profiling modes.

Parameters:
mode the profiling mode
profileData
runtime
See also:
org.jruby.Ruby.printProfileData(org.jruby.runtime.profile.ProfileData)
 
     public static ProfilePrinter newPrinter(ProfilingMode modeProfileData profileData) {
         return newPrinter(modeprofileDatanull);
     }
 
     static ProfilePrinter newPrinter(ProfilingMode modeProfileData profileDataInvocation topInvocation) {
         final ProfilePrinter printer;
         if (topInvocation == nulltopInvocation = profileData.computeResults();
         if (mode == .) {
             printer = new FlatProfilePrinter(profileDatatopInvocation);
         }
         else if (mode == .) {
             printer = new GraphProfilePrinter(profileDatatopInvocation);
         }
         else if (mode == .) {
             printer = new HtmlProfilePrinter(profileDatatopInvocation);
         }
         else if (mode == .) {
             printer = new JsonProfilePrinter(profileDatatopInvocation);
         }
         else {
             printer = null;
         }
         return printer;
     }
     
     private final ProfileData profileData;
     private final Invocation topInvocation;
     
     protected ProfilePrinter(ProfileData profileData) {
         this(profileDataprofileData.computeResults());
     }
 
     protected ProfilePrinter(ProfileData profileDataInvocation topInvocation) {
         this. = profileData;
         this. = topInvocation;
     }
 
     public ProfileData getProfileData() {
         return ;
     }
     
     protected Invocation getTopInvocation() {
         return ;
     }
 
     public void printHeader(PrintStream out) { }
     public void printFooter(PrintStream out) { }
     
    public void printProfile(PrintStream out) {
        printProfile(outfalse);
    }
    public abstract void printProfile(PrintStream outboolean first) ;
    public void printProfile(RubyIO out) {
        printProfile(new PrintStream(out.getOutStream()));
    }
    boolean isProfilerInvocation(Invocation inv) {
        return isThisProfilerInvocation(inv.getMethodSerialNumber()) || 
                (inv.getParent() != null && isProfilerInvocation(inv.getParent()));
    }
    
    boolean isThisProfilerInvocation(int serial) {
        final String start = ;
        final String stop = ;
        
        final String name = methodName(serial);
        return ( name.hashCode() == start.hashCode() && name.equals(start) ) ||
               ( name.hashCode() == stop.hashCode() && name.equals(stop) );
    }
    public String getThreadName() {
        if (getProfileData().getThreadContext().getThread() == null) {
            return Thread.currentThread().getName();
        } else {
            return getProfileData().getThreadContext().getThread().getNativeThread().getName();
        }
    }
    public String methodName(int serial) {
        return .methodName(serial);
    }
    static String methodName(ProfiledMethod profileMethod) {
        final String displayName;
        if (profileMethod != null) {
            String name = profileMethod.getName();
            DynamicMethod method = profileMethod.getMethod();
            if (name == nullname = method.getName();
            displayName = moduleHashMethod(method.getImplementationClass(), name);
        } else {
            displayName = "<unknown>";
        }
        // System.out.printf("%d - %s\n", serial, displayName);
        return displayName;
    }
    
    protected static IntHashMap<MethodDatamethodData(Invocation top) {
        IntHashMap<MethodDatamethods = new IntHashMap<MethodData>();
        MethodData data = new MethodData(0);
        methods.put(0, data);
        data.invocations.add(top);
        methodData1(methodstop);
        return methods;
    }
    private static void methodData1(final IntHashMap<MethodDatamethodsInvocation inv) {
        for (Entry<Invocationentry : inv.getChildren().entrySet()) {
            Invocation child = entry.getValue();
            int serial = child.getMethodSerialNumber();
            MethodData data = methods.get(serial);
            if (data == null) {
                data = new MethodData(serial);
                methods.put(serialdata);
            }
            data.invocations.add(child);
            methodData1(methodschild);
        }
    }
    private static final String PROFILER_START_METHOD = "JRuby::Profiler.start";
    private static final String PROFILER_STOP_METHOD = "JRuby::Profiler.stop";
    
    /*
     * Here to keep these in one place if the hash format gets updated
     * @see ProfileData#computeResults()
     */
    static final String PROFILER_PROFILE_METHOD = "JRuby::Profiler.profile";
    static final String PROFILER_PROFILED_CODE_METHOD = "JRuby::Profiler.profiled_code";
    
    private static String moduleHashMethod(RubyModule moduleString name) {
        if (module instanceof MetaClass) {
            IRubyObject obj = ((MetaClassmodule).getAttached();
            if (obj instanceof RubyModule) {
                return ((RubyModuleobj).getName() + "." + name;
            } 
            if (obj instanceof RubyObject) {
                return ((RubyObjectobj).getType().getName() + "(singleton)#" + name;
            }
            return "unknown#" + name;
        }
        if (module.isSingleton()) {
            return ((RubyClassmodule).getRealClass().getName() + "(singleton)#" + name;
        }
        if (module instanceof RubyClass) {
            return module.getName() + "#" + name// instance method
        }
        return module.getName() + "." + name// module method
    }
    
    protected static void pad(PrintStream outint sizeString body) {
        pad(outsizebodytrue);
    }
    
    protected static void pad(PrintStream outint sizeString bodyboolean front) {
        if (front) {
            for (int i = 0; i < size - body.length(); i++) {
                out.print(' ');
            }
        }
        out.print(body);
        if (!front) {
            for (int i = 0; i < size - body.length(); i++) {
                out.print(' ');
            }
        }
    }
    protected static String nanoString(long nanoTime) {
        DecimalFormat formatter = new DecimalFormat("##0.00");
        return formatter.format((doublenanoTime / 1.0E9);
    }
    
New to GrepCode? Check out our FAQ X