1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 11:40:29 +00:00

Add support for tracking fs, turtle and peripheral operations

See #25
This commit is contained in:
SquidDev 2018-04-17 09:22:26 +01:00
parent bfeafe163f
commit 911e404bfa
9 changed files with 62 additions and 5 deletions

View File

@ -28,6 +28,7 @@ import dan200.computercraft.core.filesystem.ComboMount;
import dan200.computercraft.core.filesystem.FileMount;
import dan200.computercraft.core.filesystem.JarMount;
import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.core.tracking.Tracking;
import dan200.computercraft.shared.command.CommandComputer;
import dan200.computercraft.shared.command.CommandComputerCraft;
import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider;
@ -472,6 +473,7 @@ public class ComputerCraft
{
ComputerCraft.serverComputerRegistry.reset();
WirelessNetwork.resetNetworks();
Tracking.reset();
}
}
@ -482,6 +484,7 @@ public class ComputerCraft
{
ComputerCraft.serverComputerRegistry.reset();
WirelessNetwork.resetNetworks();
Tracking.reset();
}
}

View File

@ -15,6 +15,7 @@ import dan200.computercraft.core.apis.handles.EncodedInputHandle;
import dan200.computercraft.core.apis.handles.EncodedOutputHandle;
import dan200.computercraft.core.filesystem.FileSystem;
import dan200.computercraft.core.filesystem.FileSystemException;
import dan200.computercraft.core.tracking.TrackingField;
import javax.annotation.Nonnull;
import java.io.InputStream;
@ -88,6 +89,7 @@ public class FSAPI implements ILuaAPI
{
// list
String path = getString( args, 0 );
m_env.addTrackingChange( TrackingField.FS_OPS );
try {
String[] results = m_fileSystem.list( path );
Map<Object,Object> table = new HashMap<>();
@ -162,6 +164,7 @@ public class FSAPI implements ILuaAPI
// makeDir
String path = getString( args, 0 );
try {
m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.makeDir( path );
return null;
} catch( FileSystemException e ) {
@ -174,6 +177,7 @@ public class FSAPI implements ILuaAPI
String path = getString( args, 0 );
String dest = getString( args, 1 );
try {
m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.move( path, dest );
return null;
} catch( FileSystemException e ) {
@ -186,6 +190,7 @@ public class FSAPI implements ILuaAPI
String path = getString( args, 0 );
String dest = getString( args, 1 );
try {
m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.copy( path, dest );
return null;
} catch( FileSystemException e ) {
@ -197,6 +202,7 @@ public class FSAPI implements ILuaAPI
// delete
String path = getString( args, 0 );
try {
m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.delete( path );
return null;
} catch( FileSystemException e ) {
@ -208,6 +214,7 @@ public class FSAPI implements ILuaAPI
// open
String path = getString( args, 0 );
String mode = getString( args, 1 );
m_env.addTrackingChange( TrackingField.FS_OPS );
try {
switch( mode )
{
@ -288,6 +295,7 @@ public class FSAPI implements ILuaAPI
// find
String path = getString( args, 0 );
try {
m_env.addTrackingChange( TrackingField.FS_OPS );
String[] results = m_fileSystem.find( path );
Map<Object,Object> table = new HashMap<>();
for(int i=0; i<results.length; ++i ) {

View File

@ -45,4 +45,9 @@ public interface IAPIEnvironment
void setLabel( String label );
void addTrackingChange( TrackingField field, long change );
default void addTrackingChange( TrackingField field )
{
addTrackingChange( field, 1 );
}
}

View File

@ -15,6 +15,7 @@ import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.ComputerThread;
import dan200.computercraft.core.computer.ITask;
import dan200.computercraft.core.tracking.TrackingField;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -109,6 +110,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
}
if( method >= 0 )
{
m_environment.addTrackingChange( TrackingField.PERIPHERAL_OPS );
return m_peripheral.callMethod( this, context, method, arguments );
}
else

View File

@ -76,7 +76,10 @@ public class ComputerTracker
void addValue( TrackingField field, long change )
{
fields.adjustOrPutValue( field, change, change );
synchronized( fields )
{
fields.adjustOrPutValue( field, change, change );
}
}
public long get( TrackingField field )
@ -86,7 +89,10 @@ public class ComputerTracker
if( field == TrackingField.TOTAL_TIME ) return totalTime;
if( field == TrackingField.AVERAGE_TIME ) return totalTime / tasks;
return fields.get( field );
synchronized( fields )
{
return fields.get( field );
}
}
public String getFormatted( TrackingField field )

View File

@ -40,13 +40,18 @@ public class TrackingContext
return true;
}
public synchronized List<ComputerTracker> getTimings()
public synchronized List<ComputerTracker> getImmutableTimings()
{
ArrayList<ComputerTracker> timings = new ArrayList<>( this.timings.size() );
for( ComputerTracker timing : this.timings ) timings.add( new ComputerTracker( timing ) );
return timings;
}
public synchronized List<ComputerTracker> getTimings()
{
return new ArrayList<>( timings );
}
public void addTiming( Computer computer, long time )
{
if( !tracking ) return;

View File

@ -14,6 +14,10 @@ public class TrackingField
public static final TrackingField AVERAGE_TIME = TrackingField.of( "average", "Average time", x -> String.format( "%4.1fms", x / 1e6 ) );
public static final TrackingField MAX_TIME = TrackingField.of( "max", "Max time", x -> String.format( "%5.1fms", x / 1e6 ) );
public static final TrackingField PERIPHERAL_OPS = TrackingField.of( "peripheral", "Peripheral calls", x -> String.format( "%6d", x ) );
public static final TrackingField FS_OPS = TrackingField.of( "fs", "Filesystem operations", x -> String.format( "%6d", x ) );
public static final TrackingField TURTLE_OPS = TrackingField.of( "turtle", "Turtle operations", x -> String.format( "%6d", x ) );
private final String id;
private final String displayName;
private final LongFunction<String> format;

View File

@ -338,7 +338,7 @@ public final class CommandComputerCraft extends CommandDelegate
{
TrackingContext timings = getTimingContext( context );
if( !timings.stop() ) throw new CommandException( "Tracking not enabled" );
displayTimings( context, timings.getTimings(), TrackingField.AVERAGE_TIME );
displayTimings( context, timings.getImmutableTimings(), TrackingField.AVERAGE_TIME );
}
} );
@ -357,7 +357,7 @@ public final class CommandComputerCraft extends CommandDelegate
if( field == null ) throw new CommandException( "Unknown field '" + arguments.get( 0 ) + "'" );
}
displayTimings( context, getTimingContext( context ).getTimings(), field );
displayTimings( context, getTimingContext( context ).getImmutableTimings(), field );
}
@Nonnull

View File

@ -15,6 +15,7 @@ import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.turtle.event.TurtleAction;
import dan200.computercraft.api.turtle.event.TurtleActionEvent;
import dan200.computercraft.core.apis.IAPIEnvironment;
import dan200.computercraft.core.tracking.TrackingField;
import dan200.computercraft.shared.turtle.core.*;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@ -161,70 +162,83 @@ public class TurtleAPI implements ILuaAPI
case 0:
{
// forward
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Forward ) );
}
case 1:
{
// back
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Back ) );
}
case 2:
{
// up
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Up ) );
}
case 3:
{
// down
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleMoveCommand( MoveDirection.Down ) );
}
case 4:
{
// turnLeft
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleTurnCommand( TurnDirection.Left ) );
}
case 5:
{
// turnRight
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleTurnCommand( TurnDirection.Right ) );
}
case 6:
{
// dig
Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Forward, side ) );
}
case 7:
{
// digUp
Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Up, side ) );
}
case 8:
{
// digDown
Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDigCommand( InteractDirection.Down, side ) );
}
case 9:
{
// place
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Forward, args ) );
}
case 10:
{
// placeUp
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Up, args ) );
}
case 11:
{
// placeDown
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtlePlaceCommand( InteractDirection.Down, args ) );
}
case 12:
{
// drop
int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Forward, count ) );
}
case 13:
@ -294,48 +308,56 @@ public class TurtleAPI implements ILuaAPI
{
// attack
Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Forward, side ) );
}
case 23:
{
// attackUp
Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Up, side ) );
}
case 24:
{
// attackDown
Optional<TurtleSide> side = parseSide( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleAttackCommand( InteractDirection.Down, side ) );
}
case 25:
{
// dropUp
int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Up, count ) );
}
case 26:
{
// dropDown
int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleDropCommand( InteractDirection.Down, count ) );
}
case 27:
{
// suck
int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Forward, count ) );
}
case 28:
{
// suckUp
int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Up, count ) );
}
case 29:
{
// suckDown
int count = parseCount( args, 0 );
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleSuckCommand( InteractDirection.Down, count ) );
}
case 30:
@ -389,11 +411,13 @@ public class TurtleAPI implements ILuaAPI
case 36:
{
// equipLeft
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleEquipCommand( TurtleSide.Left ) );
}
case 37:
{
// equipRight
m_environment.addTrackingChange( TrackingField.TURTLE_OPS );
return tryCommand( context, new TurtleEquipCommand( TurtleSide.Right ) );
}
case 38: