Method to check for free cogs?
T Chap
Posts: 4,223
I am looking at the manual at cog related stuff, but don't see what looks like a method to at any given time poll the Propeller and return what cogs are currently used. What is a quick way to scan each cog to see who is occupied?
Comments
You can save that in a variable for later use.
Edit: see also 'cogid'
If you're doing this to find a cog number to use with coginit, please put this out of your mind posthaste. There is rarely, if ever, a good reason to use coginit. Always use cognew to start a new cog.
-Phil
There is no direct support for that - neither from SPIN nor from PASM. If you want to keep track of the started COGs you need a central place (say the end of RAM) to set a byte if you start a COG and clear it if the COG is stopped/stops itself. But for all the Object Exchange objects you use, you have to modify that code accordingly.
Post Edited (MagIO2) : 5/29/2009 9:38:29 PM GMT
A cognew loop is nice to find out which COGs are left. You could start a very little PASM program that simply terminates immediately, so you don't have to waste SPIN-time to do a COGSTOP.
Post Edited (MagIO2) : 5/29/2009 9:46:26 PM GMT
-Phil
ATM I'm in the progress of writing a driver which needs cogs synchronised in a way which can't be achieved with waitcnt & Co, e.g. I need 2 cogs which come out of their hub access window N cycles apart. This means I could pick the first one through cognew but the second one has to be by coginit. And allocating all currently available cogs and run tests for a matching pair is not an option.
Post Edited (TChapman) : 5/30/2009 1:13:56 AM GMT
You may well have identified an exception to the no-coginit rule, however, but it's hard to tell without more info. And, to be honest, I've used it myself — once — in a bootloader, but only because some end-users may have certain (unreasonable) expectations about which cog their top-level program starts in.
Although I may be one of the more outspoken critics of coginit, I'm certainly not alone. This exchange, for example, may help to illustrate the point. (Sorry, Chip. Sometimes, ya just gotta call in the big guns! I hope you haven't changed your mind in the meantime.)
-Phil
1. Assume neither you or your COG checker program has any knowledge of the other programs that may be running in the Prop. They are "black boxes".
2. Assume there is a COG checker that returns a list of free COGS (or even just the first free COG). Lets say using Phils technique or whatever.
3. The caller of the COG checker would now like to use one of the reported free COGs.
BUT in the time between steps 2) and 3) the rest of the application (in the black box) may well have already started to use that very COG. BANG your app is dead.
On this basis it looks to be that if you have to check for free COGs your in trouble.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
So why would you need to inventory cog use? In a development environment, using foreign objects, it might provide info that ferreting out by analytic means would take longer to obtain. Or you might have an object that can start a variable number of cogs for speed or efficiency and use the inventory to optimise asset allocation dynamically. There may be other valid reasons; but using the info to pick and choose a free cog to start is not one of them.
-Phil
Why would you assume that a COGSTOP takes any significant time at all, or that a COGNEW takes more time than a COGINIT? But that's beside the point. If a cog routine is temporary and has to play nice with its timeshared cogmates, it should be programmed to exit gracefully rather than have to be suffocated with a COGINIT or bludgeoned to death with a COGSTOP. OTOH, suicide is okay. If a program wants to end its own cog life with a COGSTOP or COGINIT, that's fine and much preferred over having it imposed externally.
I suppose it's a minor point; and, of course, there's never a rule without an exception; but in my mind a COGINIT, from a style standpoint, is no different than a GOTO. Sure, there are times when a GOTO might seem the quickest solution to a flow-of-control problem; but that's a slippery slope, and if there's a solution that doesn't involve such lack of finesse, it's always better — even if it involves a little more code or takes a little longer to execute.
A program has to do more than just work. Style matters. And despite my loudly touted preferences, I don't want to be the arbiter of style. Programming style is a cultural phenomenon that evolves over time and can't be dictated — except by what gets included with, or excluded from, the language itself.
-Phil
1. call stop function of the driver I want to stop -> SPIN function call
2. driver stop function uses cogstop -> SPIN instruction
3. call start function of the driver I want to start instead -> SPIN function call
4. driver start first calls stop in most cases or checks if COG already started -> SPIN function call / SPIN if statement
5. driver start uses cogstart to run the COG-code -> SPIN instruction
my version could be (it depends on whether I need that extra speed of course)
1. call coginit with the driver I want to start using the COG-ID of the COG I want to stop
Anyway ... if you want to know how many COGs are free at any time, here is how you can count:
can't second this. We have 160MIPs under the hood. So, assuming you add some external RAM you can do very nice things with one Propeller. And depending on the system we talk about, it might be useful to get rid of a driver that you currently don't need and use it differently.
I want to build some kind of couch potato device ;o) It will have a nice menu system with a lot of functions I currently can't even think of. So, when I select something from the menu it has to do something completely different than before. Why not get rid of the COGs that only have been started for the previous function and use em again? And why waste the code-space to implement a graceful shutdown? You need code in the main-controller to notify the COG that it should stop and you need code in the COG to recognize the notification. As long as we don't talk about a COG that writes to flash memory I don't see a good reason for that.
What you're forgetting is that the stop routines of many driver objects do more than just free a cog. There may also be some other housekeeping chores that have to be performed to effect a graceful exit. I'm sorry to disagree, but it's certainly not an approach I would use in my own programming.
-Phil
no need to apologize for disagreing. It's your opinion and mine is not so far away from yours. I only allow exceptions and you want to tell us that it's a devils tool. Again: NEVER use coginit in ObjectExchange code (here the hurdles for being a good reason of an exception are much higher ;o)
The basic idea behind the prop is that you use cogs to do things that would be done by interrupt service routines in a more normal processor. Now quite often those things won't take all the available bandwidth; it certainly doesn't occupy a 20 MIPS processor full-time to read a keyboard, but in the prop that's the usual configuration. And the prop is cleverly built so that what you lose in MIPs, you gain in lessened power consumption as you WAITCNT. The four port serial driver is a clever exception but I think it IS an exception because virtual UARTs are a particularly simple case to emulate.
Anyway, there's no point in us having a strenuous disagreement; each of us pursuing our focus is likely to develop tools the other might find surprisingly useful in unanticipated contexts. I think a cog inventory would be a useful thing, and it seems that this would benefit your programming style even more than mine. So getting back to the thread topic I'd guess we are pretty much in agreement.
Another usecase for coginit - even for a driver: You have a driver where configuration changes periodicaly but the dirver is so busy that you don't want it to check for new config by itself.
-Phil