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.FileMount;
import dan200.computercraft.core.filesystem.JarMount; import dan200.computercraft.core.filesystem.JarMount;
import dan200.computercraft.core.terminal.Terminal; import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.core.tracking.Tracking;
import dan200.computercraft.shared.command.CommandComputer; import dan200.computercraft.shared.command.CommandComputer;
import dan200.computercraft.shared.command.CommandComputerCraft; import dan200.computercraft.shared.command.CommandComputerCraft;
import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider; import dan200.computercraft.shared.common.DefaultBundledRedstoneProvider;
@ -472,6 +473,7 @@ public class ComputerCraft
{ {
ComputerCraft.serverComputerRegistry.reset(); ComputerCraft.serverComputerRegistry.reset();
WirelessNetwork.resetNetworks(); WirelessNetwork.resetNetworks();
Tracking.reset();
} }
} }
@ -482,6 +484,7 @@ public class ComputerCraft
{ {
ComputerCraft.serverComputerRegistry.reset(); ComputerCraft.serverComputerRegistry.reset();
WirelessNetwork.resetNetworks(); 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.apis.handles.EncodedOutputHandle;
import dan200.computercraft.core.filesystem.FileSystem; import dan200.computercraft.core.filesystem.FileSystem;
import dan200.computercraft.core.filesystem.FileSystemException; import dan200.computercraft.core.filesystem.FileSystemException;
import dan200.computercraft.core.tracking.TrackingField;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.InputStream; import java.io.InputStream;
@ -88,6 +89,7 @@ public class FSAPI implements ILuaAPI
{ {
// list // list
String path = getString( args, 0 ); String path = getString( args, 0 );
m_env.addTrackingChange( TrackingField.FS_OPS );
try { try {
String[] results = m_fileSystem.list( path ); String[] results = m_fileSystem.list( path );
Map<Object,Object> table = new HashMap<>(); Map<Object,Object> table = new HashMap<>();
@ -162,6 +164,7 @@ public class FSAPI implements ILuaAPI
// makeDir // makeDir
String path = getString( args, 0 ); String path = getString( args, 0 );
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.makeDir( path ); m_fileSystem.makeDir( path );
return null; return null;
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
@ -174,6 +177,7 @@ public class FSAPI implements ILuaAPI
String path = getString( args, 0 ); String path = getString( args, 0 );
String dest = getString( args, 1 ); String dest = getString( args, 1 );
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.move( path, dest ); m_fileSystem.move( path, dest );
return null; return null;
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
@ -186,6 +190,7 @@ public class FSAPI implements ILuaAPI
String path = getString( args, 0 ); String path = getString( args, 0 );
String dest = getString( args, 1 ); String dest = getString( args, 1 );
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.copy( path, dest ); m_fileSystem.copy( path, dest );
return null; return null;
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
@ -197,6 +202,7 @@ public class FSAPI implements ILuaAPI
// delete // delete
String path = getString( args, 0 ); String path = getString( args, 0 );
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS );
m_fileSystem.delete( path ); m_fileSystem.delete( path );
return null; return null;
} catch( FileSystemException e ) { } catch( FileSystemException e ) {
@ -208,6 +214,7 @@ public class FSAPI implements ILuaAPI
// open // open
String path = getString( args, 0 ); String path = getString( args, 0 );
String mode = getString( args, 1 ); String mode = getString( args, 1 );
m_env.addTrackingChange( TrackingField.FS_OPS );
try { try {
switch( mode ) switch( mode )
{ {
@ -288,6 +295,7 @@ public class FSAPI implements ILuaAPI
// find // find
String path = getString( args, 0 ); String path = getString( args, 0 );
try { try {
m_env.addTrackingChange( TrackingField.FS_OPS );
String[] results = m_fileSystem.find( path ); String[] results = m_fileSystem.find( path );
Map<Object,Object> table = new HashMap<>(); Map<Object,Object> table = new HashMap<>();
for(int i=0; i<results.length; ++i ) { for(int i=0; i<results.length; ++i ) {

View File

@ -45,4 +45,9 @@ public interface IAPIEnvironment
void setLabel( String label ); void setLabel( String label );
void addTrackingChange( TrackingField field, long change ); 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.Computer;
import dan200.computercraft.core.computer.ComputerThread; import dan200.computercraft.core.computer.ComputerThread;
import dan200.computercraft.core.computer.ITask; import dan200.computercraft.core.computer.ITask;
import dan200.computercraft.core.tracking.TrackingField;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -109,6 +110,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
} }
if( method >= 0 ) if( method >= 0 )
{ {
m_environment.addTrackingChange( TrackingField.PERIPHERAL_OPS );
return m_peripheral.callMethod( this, context, method, arguments ); return m_peripheral.callMethod( this, context, method, arguments );
} }
else else

View File

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

View File

@ -40,13 +40,18 @@ public class TrackingContext
return true; return true;
} }
public synchronized List<ComputerTracker> getTimings() public synchronized List<ComputerTracker> getImmutableTimings()
{ {
ArrayList<ComputerTracker> timings = new ArrayList<>( this.timings.size() ); ArrayList<ComputerTracker> timings = new ArrayList<>( this.timings.size() );
for( ComputerTracker timing : this.timings ) timings.add( new ComputerTracker( timing ) ); for( ComputerTracker timing : this.timings ) timings.add( new ComputerTracker( timing ) );
return timings; return timings;
} }
public synchronized List<ComputerTracker> getTimings()
{
return new ArrayList<>( timings );
}
public void addTiming( Computer computer, long time ) public void addTiming( Computer computer, long time )
{ {
if( !tracking ) return; 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 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 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 id;
private final String displayName; private final String displayName;
private final LongFunction<String> format; private final LongFunction<String> format;

View File

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